Redirecting page links after .htaccess SEO upgrade? [closed]

此生再无相见时 提交于 2019-12-08 15:18:50

问题


I didn't want to bloat one post here with a ton of .htaccess questions, so I'm breaking them up individually. This one has to do with using RedirectMatch to create rules for existing, indexed pages that are part of an existing website.

Specifically, my test site has been up for about a year and I am now using .htaccess to re-write clean URLs for better SEO. So when I had a container page whose content was called based on a query string in a link, like so:

http://www.website.com/departments.php?dep=contact_us

I'm working on making the URL appear smoother like:

http://www.website.com/contact-us/

The tutorial I read suggested that changing my links to the new, snazzier SEO-friendly style would lead to lots of 404 errors and broken page links until the site has been indexed again and the records brought up to date.

Thus, my question: Is this a legitimate concern or will the new links be picked up in 24-48 hours?

I ask because I have several websites with many layers of file structure and I'm thinking that I'd need a TON of rules in my .htaccess file to fix all the broken links I may be creating.

If I'm creating confusion here, please let me know and I'll clarify things.

Many thanks in advance!


回答1:


Thus, my question: Is this a legitimate concern or will the new links be picked up in 24-48 hours?

You can make it happen sooner if you externally redirect the client using a 301 (permanent). This is a lot trickier than internally rewriting on the server's end because you'll cause conflicting rules. For example, if you wanted to rewrite url-a to url-b internally on the server, then redirect the browser from url-b to url-a (meaning when a client specifically requests url-b to redirect it to url-a):

# internally rewrite url-a (fake URL) to url-b (where the actual content is)
RewriteRule ^url-a$ /url-b [L]

# externally redirect url-b (actual content) to url-a (fake URL)
RewriteRule ^url-b$ /url-a [L,R=301]

Since the rewrite engine loops, these 2 rules will continue to rewrite each other and cause a 500 internal server error (the same thing happens if you replace the second rule with a RedirectMatch). To fix this, you need to create a condition so that the external redirect (second rule) only gets applied if the actual request was for "url-b". You can do this by matching against the %{THE_REQUEST} variable, which is essentially the first line of an HTTP request.

Using your example URLs, you'd have something like this:

RewriteRule ^contact-us/?$ /departments.php?dep=contact_us [L]

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /departments\.php\?dep=contact_us
RewriteRule ^ /contact-us/ [L,R=301]

This means when someone like a google-bot attempts to resolve http://www.website.com/departments.php?dep=contact_us, the 2nd rule's condition will match the request (which will look something like: GET /departments.php?dep=contact_us HTTP/1.1) and it will get redirected to http://www.website.com/contact-us/. At which point, the client (google-bot) will request /contact-us/ and the first rule will get applied and internally rewrite the URI to /departments.php?dep=contact_us.



来源:https://stackoverflow.com/questions/15891620/redirecting-page-links-after-htaccess-seo-upgrade

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!