将旧网址重写为漂亮的网址,并使用 .htaccess 将旧网址重定向到新网址
Rewriting old urls to pretty urls, and redirect old urls to new with .htaccess
我在 .htaccess
设置方面遇到一些问题。
我的网站主页 url 是:http://www.domain.com/b/en/index.php ,and I use mod_rewrite
to create pretty url : http://www.domain.com/en
这是我对 urls 的重写规则(有效):
RewriteRule ^en/(.*)$ /b/ [L,QSA,NC,PT]
那是我想要的 url,但是当我将 "old urls" 重定向到新的 url 时,出现了一些问题。
我的 Chrome 浏览器显示“www.domain.com redirected you too many times.
”
这是我的旧 url 重定向到新规则:
RewriteRule ^b/en/(.*)$ /en/ [NC,L,R=301]
这是一个一般的两部分问题。您需要将原始请求重定向到更漂亮的请求:
RewriteRule ^en/(.*)$ /b/en/ [L,QSA,NC]
RewriteCond %{THE_REQUEST} ^GET\ /b/(en)/(\S*) [NC]
RewriteRule ^b/ /%1/%2 [R=301,L]
在测试新规则之前,请务必清除浏览器的缓存。
我有一个更通用的用例,我不得不稍微修改一下@hjpotter92 的解决方案。
旧URL结构:http://example.com/index.php?page=foo
新 URL 结构:http://example.com/foo
所以我想让 URLs "pretty"(即 SEO 友好)但也让旧的(丑陋的)URLs 重定向到新的 URLs 所以旧书签仍然有效。
这是我的解决方案:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/?(.*)$ /index.php?page= [L,QSA,NC]
RewriteCond %{THE_REQUEST} ^GET\ \/?index\.php\?page=(\S*) [NC]
RewriteRule ^index.php /%1? [R=301,L]
我在 .htaccess
设置方面遇到一些问题。
我的网站主页 url 是:http://www.domain.com/b/en/index.php ,and I use mod_rewrite
to create pretty url : http://www.domain.com/en
这是我对 urls 的重写规则(有效):
RewriteRule ^en/(.*)$ /b/ [L,QSA,NC,PT]
那是我想要的 url,但是当我将 "old urls" 重定向到新的 url 时,出现了一些问题。
我的 Chrome 浏览器显示“www.domain.com redirected you too many times.
”
这是我的旧 url 重定向到新规则:
RewriteRule ^b/en/(.*)$ /en/ [NC,L,R=301]
这是一个一般的两部分问题。您需要将原始请求重定向到更漂亮的请求:
RewriteRule ^en/(.*)$ /b/en/ [L,QSA,NC]
RewriteCond %{THE_REQUEST} ^GET\ /b/(en)/(\S*) [NC]
RewriteRule ^b/ /%1/%2 [R=301,L]
在测试新规则之前,请务必清除浏览器的缓存。
我有一个更通用的用例,我不得不稍微修改一下@hjpotter92 的解决方案。
旧URL结构:http://example.com/index.php?page=foo
新 URL 结构:http://example.com/foo
所以我想让 URLs "pretty"(即 SEO 友好)但也让旧的(丑陋的)URLs 重定向到新的 URLs 所以旧书签仍然有效。
这是我的解决方案:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/?(.*)$ /index.php?page= [L,QSA,NC]
RewriteCond %{THE_REQUEST} ^GET\ \/?index\.php\?page=(\S*) [NC]
RewriteRule ^index.php /%1? [R=301,L]