Forum Moderators: phranque

Message Too Old, No Replies

Trailing slash results in 404 error

         

MockY

6:43 pm on May 9, 2011 (gmt 0)

10+ Year Member



I'm trying to adhere to the SEO requirements my company now want to have on their sites. One of those is to show pages without the extension and with a trailing slash.
Old way: www.website.com/page.php
New way: www.website.com/page/
The new way should be for every page used by the sites.

I have tested many .htaccess rules and the one I currently have in place is the only one that successfully automatically adds a trailing slash if the page extension is omitted:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.website.com/test/$1/ [L,R=301]

Right now i'm just testing it, so there is one file in the test directory called page.php.

When hitting the page with the file extension, the page is displayed, but when the .php is omitted, the trailing slash is added but apache is returning a 404 error.

I need some assistance in order to get this running like it should.

phranque

7:07 am on May 10, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, MockY!

you got the external redirect to the non-extension, trailing-slash url working.
now you have to add the internal rewrite so that it removes the trailing slash and appends the extension.

g1smd

10:27 am on May 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The HTTP specs define a URL with a trailing slash as being for a folder or for the index page in a folder.

If the URL is for a page, then the trailing slash should be OMITTED. Why do you want to break the HTTP specification?

You now need a rewrite so that when a URL for a page is requested, the server looks for the matching file with .php extension appended. However, if you insist on using URLs with a trailing slash for pages, Apache will not know if the request is for a real folder on the site or for one of your "pages". In that case you will need to add a RewriteCond with "-d" that checks to see if the request is really matched by a physical folder on the server. Those checks are very slow and inefficient - the topic of hundreds of posts over the last few years.

If you follow the HTTP specification and do NOT add a slash to page URLs, you can instead have a very fast and efficient RegEx pattern to test for page URLs because those URLs have no extension and no slash.

DirectoryIndex index.php
RewriteEngine On
#
# Redirect requests for index.php URLs to URL for folder, ending in slash.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^test/(([^/]+/)*)index\.php$ http://www.example.com/test/$1 [R=301,L]
#
# Redirect requests for .php URLs to matching extensionless URL without slash.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*[^/.]+\.php\ HTTP/
RewriteRule ^test/(([^/]+/)*[^/.]+)\.php$ http://www.example.com/test/$1 [R=301,L]
#
# Rewrite extensionless URL requests to internally find the respective .php file.
RewriteRule ^test/(([^/]+/)*[^/.]+)$ /$1.php [L]


Your original code would also have seen URLs like
www.example.com/test/folder/index/
as being valid URLs on your site. The canonical URL should be
www.example.com/test/folder/
.