Bricktop Posted February 28, 2008 Share Posted February 28, 2008 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 More sharing options...
obsidian Posted February 28, 2008 Share Posted February 28, 2008 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. Link to comment https://forums.phpfreaks.com/topic/93506-rewrite-and-redirect-same-page/#findComment-479080 Share on other sites More sharing options...
Bricktop Posted February 28, 2008 Author Share Posted February 28, 2008 Thank you Obsidian - very helpful. All my site-links are pointing to the new Rewritten paths nayway so in time I'm sure the Google back-links will update also. Thanks again, and I will agree with you and leave everything as it is Link to comment https://forums.phpfreaks.com/topic/93506-rewrite-and-redirect-same-page/#findComment-479135 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.