我有一个 PHP 网站,它使用 GET 变量来确定它应该做什么......
例如/index.php?s=about&p=2
看来我应该能够使用重写规则来更改它,这样我就可以使用如下 URL:
/about/2
我怎样才能得到这种行为?
我有一个 PHP 网站,它使用 GET 变量来确定它应该做什么......
例如/index.php?s=about&p=2
看来我应该能够使用重写规则来更改它,这样我就可以使用如下 URL:
/about/2
我怎样才能得到这种行为?
/about/2
.您需要将任何相对链接调整为绝对链接或将其添加到所有页面的标题中:
<base href="/">
将这些规则添加到文档根目录中的 htaccess 文件中,以将漂亮的链接改回丑陋的链接:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/? /index.php?s=$1&p=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/? /index.php?s=$1 [L]
为了使搜索引擎可以重新索引您的页面,您需要通过将其添加到同一个 htaccess 文件中来将旧的丑陋的 301 重定向到漂亮的页面:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?s=([^&]+)&p=([^&]+)
RewriteRule ^index.php$ /%1/%2 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?s=([^&]+)
RewriteRule ^index.php$ /%1 [L,R=301]