问题描述
我有一些由 Google 以 https://example.com/1234/name-of-page
格式编入索引的旧网址
因为我不再使用这些 RewriteRules,所以我想将它们重新路由到 https://example.com?id=1234&name=name-of-page
如何使用 .htaccess 执行此操作?
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^([^/]*)/(.*)/?$ https://%{HTTP_HOST}/title.php?id=$1&name=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sitemap\.xml/?$ sitemap.php
解决方法
如果您正在寻找要重写的从 1234/name-of-page
开始的 uri,请尝试以下操作。请确保在测试您的网址之前清除浏览器缓存。
RewriteEngine ON
RewriteRule ^(1234)/(name-of-page)/?$ title.php?id=$1&name=$2 [L]
对于更通用的规则,您可以尝试遵循。请确保您一次尝试以上或仅在 .htaccess 文件中遵循规则。
RewriteEngine ON
RewriteRule ^([^/]*)/(.*)/?$ title.php?id=$1&name=$2 [L]