Forum Moderators: phranque
This is probably a simple question but is bugging the hell out of me.
Here is the code...
RewriteRule ^portfolio$ /portfolio.php [NC]
I just want to strip the file of the .php extension and if someone types in 'portfolio.php' to automatically redirect to 'portfolio'. At the moment with this, the stripped version works, but so does the version with the .php extension.
I tried doing the same thing with another page...
RewriteRule ^contactus$ /contactus.php [R=301,L]
But this does the opposite of what I want. I type in 'www.mysite/contactus', and it redirects to 'contactus.php'
If anyone can let me know how to go about what Im after it will be much appretiated.
Cheers
Have you tried reversing the pattern and substitution in the rule, then?
^contactus\.php$ /contactus [R=301,L]
For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].
Jim
now it is doing what i intended... redirecting contactus.php to contactus, however i have a '404 Not Found' at contactus. How do I tell it to display the origional contactus.php file in its place?
Sorry if this is a stupid question its my 1st attempt at using .htaccess
Thanks
There's a trick to doing this: Examining the original client request as it was received, prior to any applied rewrites, in order to prevent such a loop. It looks more complicated than it is, really.
# Internally rewrite extensionless URL to actual file
RewriteRule ^contacus$ /contactus.php [L]
#
# Exrernally redirect client requests for URL.php to extension-less URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /contactus\.php
RewriteRule ^contactus\.php$ http://www.example.com/contactus [R=301,L]
This is necessary in a .htaccess context, because module processing will be restarted after any rewrites are done, so that the server-level accessc controls can be applied to the newly-rewritten URL. Otherwise, the server could be easily hacked. It's important to understand that in .htaccess context, the [L] flag means, "This is the last rule to be processed on this pass through .htaccess".
Note that you should change all links on your pages to point to "contactus", omitting the ".php". That, combined with the action of the 301 redirect in the code above, will establish "contactus" as the canonical URL for that page in search engine listings.
For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].
Jim
I want '/browse.php?id=236' to show up as '/browse/236'... along with the redirect too, so i tried by writing this:
RewriteRule ^browse([0-9]+)/?$ /browse.php?id=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /browse\.php?id=$1
RewriteRule ^browse\.php?id=$1 /browse([0-9]+)/?$ [R=301,L] Can you tell me what Im doing wrong, or if i should be doing something else for this?
also for the occasional URL, if it is for example '/browse.php?id=249' is it possible to change it to '/browse/black-opal'?
thanks Jim