Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite problem

         

opalplus

1:08 am on Oct 10, 2006 (gmt 0)

10+ Year Member



Hi guys,

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

jdMorgan

1:15 pm on Oct 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> But this does the opposite of what I want.

Have you tried reversing the pattern and substitution in the rule, then?


^contactus\.php$ /contactus [R=301,L]

The pattern on the left, if it matches with the URL requested by the browser, will invoke a redirect to the substitution URL on the right.

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

opalplus

1:51 am on Oct 11, 2006 (gmt 0)

10+ Year Member



awesome thanks man... i tried switching around just forgot the damn backslash the 1st time!

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

jdMorgan

1:35 pm on Oct 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, that complicates things, because you can't rewrite both ways at once in .htaccess without creating an 'infinite' loop, triggering a 'maximum redirection limit exceeded' server error or a similarly-worded browser error.

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]

By checking that the "contactus.php" request originated at the client (browser), rather than as a result of the first rule being applied, the loop can be avoided.

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

opalplus

12:46 am on Oct 12, 2006 (gmt 0)

10+ Year Member



Jim your a champion!

Awesome, it works perfectly... exactly the way I wanted it to plus I understand now what you did aswell! im stoked!

thanks mate

opalplus

1:03 am on Oct 12, 2006 (gmt 0)

10+ Year Member



sorry man i thought i understood and tried to go it alone for something else but ran into trouble (maybe too much too soon)...

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

jdMorgan

12:51 am on Oct 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See this thread [webmasterworld.com] in our forum library for an explanation of the process.

Jim

opalplus

3:08 am on Oct 13, 2006 (gmt 0)

10+ Year Member



cool thanks mate, have had a quick scan over it and looks like just wat i need.

cheers