我有一个网站,其中包含about.php,contact.php和test.php?id=xyz等页面。
我添加了以下重定向以使其对 seo 友好。
Options -Indexes -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www'.krishibazar'.org$ [NC]
RewriteRule ^(.*)$ http://www.krishibazar.org/$1 [R=301,L]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} 's/+(?:index)?(.*?)'.php['s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1'.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
这给了我
mydomain.com => www.mydomain.com
mydomain.com/about.php => www.mydomain.com/about
mydomain.com/test.php?id=12 => www.mydomain.com/test?id=12
mydomain.com/about/ => CSS Error
它工作得很好,除非我在最后一个给出错误处添加一个尾部斜杠。因此,要么建议我防止错误,要么添加尾部斜杠。
选项 1:去掉斜杠(对 SEO 有好处)
如何附加规则来处理带有尾部斜杠的 URL?
我会把它放在上面的externally
和internally
重定向之间,如下所示:
RewriteRule ^(.*)/$ /$1 [L,R=301]
这应该将所有 URL 重定向到它们的无斜杠版本,然后再将它们重写为 PHP 文件。
选项 2:修复您的 CSS
我想您相对引用样式表,例如
<style href="css/style.css" />
您可以将其更改为绝对引用
<style href="/css/style.css" />
无论假定的目录深度如何(在 URL 中用斜杠表示),它都应该有效。
结论
在这种情况下,我会选择选项 1,因为它为所有内容提供了一个 URL,在我看来这是可取的。