Forum Moderators: phranque
#redirect whatever.php to /whatever
RewriteRule ^([a-z_-]+)\.(php¦html¦htm¦asp)$ $1 [R=301,L]#rewrite /whatever to whatever.php
RewriteRule ^([a-z_-]+)$ $1.php
I need to redirect people to /page1 so they can't never see the extensions but only the links without trailing slashes.
The problem with thoserules is that when the second one tries to read from page.php (that exists) it gets the first rule applied too, causing an endless loop.
What condition can I use to prevent the "internal" rewriting to consider the first rule (which applies to navigation only)?
Thank you
Plus, when can I find a full guide with the expiations of all expressions (like [L] [NC] / ^ * )?
[edited by: Sandro87 at 11:03 pm (utc) on May 16, 2009]
# Externally redirect only direct client requests for <whatever>.php to /<whatever>
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[a-z_\-]+\.(php¦html?¦asp)(#[^?\ ]*)?(\?[^\ ]*)?\ HTTP/
RewriteRule ^([a-z_\-]+)\.(php¦html?¦asp)$ $1 [R=301,L]
#
# Internally rewrite /<whatever> to <whatever>.php
RewriteRule ^([a-z_\-]+)$ $1.php [L]
> Plus, when can I find a full guide with the expiations of all expressions (like [L] [NC] / ^ * )?
See the resources cited in our Forum Charter (link at top of this page).
Jim
[edited by: jdMorgan at 1:38 am (utc) on May 17, 2009]
The redirect will be invoked only if the client requested the URL-path with a filetype extension, and not as a result of the extension being added by your second rule.
Jim
GET test.php?var=val HTTP/1.1
There may be a typo in the code I posted, but the idea is based on code that has been deployed to hundreds or thousands of servers and it works fine, so any problem here is likely to be subtle. If you have made *any* changes to the code I posted other than fixing the broken pipes, then please re-post the code here for review.
Jim
#redirect whatever.php to /whatever
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[a-z_\-]+\.(php¦html?¦asp)([#?][^\ ]*)?\ HTTP/
RewriteRule ^([a-z_-]+)\.(php¦html¦htm¦asp)$ $1 [R=301,L]
Output
127.0.0.1 - - [17/May/2009:17:16:34 +0200] "GET /dttitalia20/news.php HTTP/1.1" 200 2277
"dttitalia20" is the root dir of the site
[edited by: Sandro87 at 3:18 pm (utc) on May 17, 2009]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[a-z_\-]+\.(php¦html?¦asp)([#?][^\ ]*)?\ HTTP/
RewriteRule ^([a-z_-]+)\.(php¦ht[b]ml?¦[/b]asp)$ [i]http://www.example.com/[/i]$1 [R=301,L]
Also, while it was helpful to show the "output" above, it doesn't mean much if you don't also show the "input." -- The less we have to guess at here, the better.
Jim
As with your original rule, this new one accepts only example.com/<filename>.<filetype> as input.
It will not accept example.com/<directory>/<filename>.<filetype>
So if you are testing by typing in "example.com/dttitalia20/news.php", then it won't redirect.
If you need to accept and redirect subdirectory paths, then you'll need to modify the rules:
# Externally redirect only direct client requests for /<whatever>.php to /<whatever>
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[b]([^/]+/)*[/b][a-z_\-]+\.(php¦html?¦asp)([#?][^\ ]*)?\ HTTP/
RewriteRule ^([b]([^/]+/)*[/b][a-z_\-]+)\.(php¦html?¦asp)$ http://www.example.com/$1 [R=301,L]
#
# Internally rewrite /<whatever> to /<whatever>.php
RewriteRule ^([b]([^/]+/)*[/b][a-z_\-]+)$ $1.php [L]
Furthermore, the filename must consist of lowercase characters, hyphens or underscores only, and obviously, only the specified filetypes will be redirected.
Please test this code with no changes whatsoever, except for fixing the broken pipe characters and putting your correct domain name in the first rule. Or, if you must make any other changes, then please post the entire code snippet back again to show your changes.
Completely flush your browser cache before testing any new server-side code.
Please let us know what URLs you typed in, and what results you got in your browser, access log, error log, etc. as applicable.
Jim