简体   繁体   English

将旧网址重写为漂亮的网址,并使用.htaccess将旧网址重定向到新网址

[英]Rewriting old urls to pretty urls, and redirect old urls to new with .htaccess

I have some problems with .htaccess settings. 我在.htaccess设置方面遇到了一些问题。 My Website home page url is : http://www.domain.com/b/en/index.php ,and I use mod_rewrite to create pretty url : http://www.domain.com/en 我的网站主页网址为: http//www.domain.com/b/en/index.php ,我使用mod_rewrite创建漂亮的网址: http//www.domain.com/en

This is my rewrite rules for pretty urls (It works): 这是我对漂亮网址的重写规则(可行):

RewriteRule ^en/(.*)$ /b/$1 [L,QSA,NC,PT]

That's pretty urls what I want, but when I redirect "old urls" to new urls, there are some problems. 这就是我想要的东西,但是当我将“旧网址”重定向到新网址时,存在一些问题。

My Chrome browser shows " www.domain.com redirected you too many times. " 我的Chrome浏览器显示“ www.domain.com redirected you too many times.

This is my old urls redirect to new rule: 这是我的旧网址重定向到新规则:

RewriteRule ^b/en/(.*)$ /en/$1 [NC,L,R=301]

This is a generic 2-part problem. 这是一个通用的两部分问题。 You'll need to redirect the raw request to prettier one: 您需要将原始请求重定向到更漂亮的请求:

RewriteRule ^en/(.*)$ /b/en/$1 [L,QSA,NC]

RewriteCond %{THE_REQUEST} ^GET\ /b/(en)/(\S*) [NC]
RewriteRule ^b/ /%1/%2 [R=301,L]

Be sure to clear your browsers' cache before testing the new rules. 在测试新规则之前,请务必清除浏览器的缓存。

I had a more generic use-case and I had to modify @hjpotter92's solution a bit. 我有一个更通用的用例,我不得不修改@ hjpotter92的解决方案。

Old URL structure: http://example.com/index.php?page=foo 旧的URL结构: http://example.com/index.php?page=foohttp://example.com/index.php?page=foo
New URL structure: http://example.com/foo 新的网址结构: http://example.com/foohttp://example.com/foo

So I wanted to make the URLs "pretty" (ie SEO-friendly) but also have the old (ugly) URLs redirect to the new URLs so old bookmarks would still work. 因此,我希望将URL设置为“漂亮”(即SEO友好),但也要将旧(丑陋)URL重定向到新URL,以便旧书签仍可使用。

Here's my solution: 这是我的解决方案:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/?(.*)$ /index.php?page=$1 [L,QSA,NC]

RewriteCond %{THE_REQUEST} ^GET\ \/?index\.php\?page=(\S*) [NC]
RewriteRule ^index.php /%1? [R=301,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM