Forum Moderators: phranque
RewriteRule ^(.*)\/$ $1.php [NC]
example.com/toy.php
becomes
example.com/toy/
This is fine except for the fact that example.com/toy (without trailing slash) returns a 404. Firefox will bookmark the website with the traing slash, IE will not. Is there anyway I can rewrite the .php URL to work for both example.com/toy and example.com/toy/ ?
[edited by: StoutFiles at 4:08 pm (utc) on Sep. 26, 2008]
Rewriting both slash- and non-slash URLs directly to the script will create duplicate content.
Add an external redirect to force the client to add or remove the slash, as desired. This will prevent the dupe-content problem.
There is no meed to escape slashes in mod_rewrite regular expressions.
Jim
If both work, and both return content with a "200 OK" status, then your site is serving Duplicate Content.
One can return content, and the other must return either a 404 error, or a 301 redirect to the canonical form.
# Externally redirect extensionless URLs to add a trialing slash (this is non-HTTP-compliant and not recommended)
RewriteCond $1 !([^/]+/)*([^.]+)$
RewriteRule ^(.*[^/])$ http://www.example.com/$1/ [R=301,L]
#
# Internally rewrite extensionless trailing-slashed URLs to php scripts
RewriteRule ^([^.]*)/$ $1.php [l]
Good luck...
Jim
example.com/toy/ in actually a homepage for toys. I rewrite ?toyvariable=ball so that the address is now example.com/toy/ball without the trailing slash.
Yes, toy.php is a file name and not a folder but it might as well be a folder with the way I'm using it. Instead of toy.php and toy.php?toyvariable=ball you get toy/ and toy/ball.
Thank you for the rewrite codes. I will look into fixing slashes in the future...the way the site is put together it will stay just as complicated now as it would be to fix slashes down the line.
[edited by: StoutFiles at 6:37 pm (utc) on Sep. 26, 2008]
Example:
example.com/toy will be rewritten or redirected to example.com/toy/
example.com/phone will not be altered.
So far I can only find examples that effect all url's, and when I try to 301 just example.com/toy to example.com/toy/ in .htaccess I get example.com/toy////////////////////// etc.
# Externally redirect direct client (browser or robot) requests
# for URLs ending in ".php" to extensionless URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://www.example.com/$1 [R=301,L]
#
# Externally redirect extensionless URLs which will resolve to existing php files
# to remove the trailing slash, except for URLs starting with "this" or "that"
RewriteCond %$1 !^(this¦that)
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)/$ http://www.example.com/$1 [R=301,L]
#
# Internally rewrite extensionless URLs to php scripts, except for URLs starting with
# "admin", "stats", or "phone", or which resolve to existing directory index pages
RewriteCond %$1 !^(admin¦stats¦phone)
RewriteCond %{REQUEST_FILENAME}/$1 !-d
RewriteRule ^(([^/]+/)*[^./]+)$ $1.php [L]
If you already have a rule to redirect direct client requests for "index.php" to "/", then it should go before the first rule here. And if you already have a rule to redirect non-canoniucal hostname requests, then it should go after the second redirect posted here. In general, put all external reidrects first, in order from most-specific to least-specific, followed by all internal rewrites, again from most-specific to least-specific.
"Most-specific" rules will have a very-specific pattern, and affect one or only a very few requests. Least-specific rules will have very general pattersn, and so will have the potential to affect many or almost all requests -- for example, the domain canonicalization redirect rule will redirect *any* request for *any* URL, of the requested hsotname is incorrect.
Jim