Htaccess SEO URLs

Hi,

I have this in my htaccess, now it this is for removing the .php extension. My subdomain is http://example.domain.com and each time htaccess tries to look for .php and shows up 404, I’ve trying to find out how to make this work as well add trailing slash http://www.domain.com/example/ when ever going to the subdir.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

ix,

Your code is fine (except for the No Case flag) for adding a .php extension to an extensionless filename. Apache will add the / to give you domain names but you will also need to check for !-d for the PHP extensionless, too. If you “merely” want to add the trailing / for directories, try:

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !/$ %{REQUEST_URI}/ [R=301,L]

Regards,

DK

I see, the rules given seems to block me from my site. The above code works, but tries to add “.php” to a directory subdomain and it gives off a 404 error. As per the trailing / can it be added to files .php or the files must be created within a folder of the same name?

Do you know where I could possibly get a list of commands? Thanks.

ix,

The rule above was designed to add a trailing / to a directory URI.

Why would anyone want to add a trailing / to a file name? This is both illogical and contradicts the functioning of the server. It IS possible, however, if you use MultiViews (which I loathe).

As for a good tutorial, I’ve had acclaims from many SP members for the one linked to in my signature (and you know where to find me if you have questions about the content).

Regards,

DK

The rule given above blocks me from my site.

ix,

Troubleshooting help requires the test URI as well as the code it’s being run against.

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !/$ %{REQUEST_URI}/ [R=301,L]

… simply says that, if the URI is a directory request and there is not a trailing /, add the trailing /. There is NOTHING there which would block anything.

So, please post your full .htaccess and test URIs.

Regards,

DK

not quite, it checks if it is an EXISTING directory:

‘-d’ (is directory)
Treats the TestString as a pathname and tests whether or not it exists, and is a directory.

httpd.apache.org/docs/2.2/mod/mod_rewrite.html

(bold is mine)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

I thought something could be added onto mine above to make it add that trailing /

The other issue is I cannot access my subdomain because it would give a 404 error for trying to add .php

ix,

Rémon is (pedantically) correct as you don’t want to redirect to a directory which does not exist. The code functions in the manner he described.

Your code says: If the {REQUEST_URI} is not for an existing file, add .php to the URI and serve that (REMOVE that No Case flag - URIs are case sensitive!!!). I do not know how you added a trailing / with this code.

Because your two cases are different (one is add .php file extension - to a non-file, i.e., DIRECTORY, name!?! - and the other is add a trailing / to a directory). As I had stressed in another current thread, your first step should have been to define each task for mod_rewrite, convert each of those tasks into pseudocode (to see where conflicts might exist) then into mod_rewrite code. You have skipped the first step and your results show it. Relax, think about each task in detail THEN get to coding.

Regards,

DK

Might you know where to find the rules given with a description, I can go off there and put stuff together.

ix,

I use apache.org as my reference for all things mod_rewrite. They’re generally readable but do get into some techno-babble but well worth wading through.

Regards,

DK