Jump to content

Recommended Posts

Hello all,

 

I'm quite new to mod_rewrite but have managed to rewrite my entire personal website.

 

However, I wonder if someone could tell me how to rewrite AND redirect the same page.

 

For example, I am rewriting /contact/ and /portfolio/ so they point to the contact.php and portfolio.php files. To do this, I have added the following lines to my .htaccess file:

 

RewriteRule ^contact/ /contact.php [L]

RewriteRule ^portfolio/ /portfolio.php [L]

 

However, some Google links and old site back-links still point to the .php links, not the new rewritten examples. I want to force any requests to the old .php files to redirect to the new rewritten links. So, if someone clicks a link to www.domain.com/contact.php they get redirected to www.domain.com/contatc/, but when I changed the .htaccess to:

 

RewriteRule ^contact.php$ /contact [L]

RewriteRule ^contact/ /contact.php [L]

RewriteRule ^portfolio.php$ /portfolio [L]

RewriteRule ^portfolio/ /portfolio.php [L]

 

When requesting either contact.php, portfolio.php, /portfolio/ or /contact/ the server would crash because I have effectively created a loop.

 

Hopefully you can understand my request and let me know how I achieve my desired result.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/93506-rewrite-and-redirect-same-page/
Share on other sites

Keep in mind that doing this with mod_rewrite really defeats the purpose, since they will still see the .php address in their browser. Mod_rewrite does everything at the server, so it is not a redirect. It literally serves the page instructed up as the path the user requests. There are additional modifiers that you can attach to a mod_rewrite to help it not to loop, but I would recommend tackling the issue by simply instructing search engines of the new structure. You can register a site-map with Google and be sure none of your current links reference the .php pages directly. This way, when you are re-indexed by the crawlers, your information will be updated.

 

It's not necessarily a problem that Google still sees those pages. The reason for mod_rewrite is typically to screen query string parameters since crawlers ignore them anyway. So, basically, on a news site, the link view.php?cat=sports&id=15 would be indexed simply as view.php. However, with a mod_rewrite, the URL looks like view/sports/15 instead, and the entire path is indexed. In your case, it really won't make much difference either way.

 

Hope this helps some.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.