.htaccess文件真的不是我的事。它们对我来说仍然是个谜,可能永远都是......
无论如何,所以我有一个.htaccess文件,对所有不同的页面都有一些重写规则。
网址 http://placeholder.com/OC/hoig/[page]
文件夹结构:
目标是为所有页面获取语义(漂亮)URL。现在,这个.htaccess文件部分工作。除了' Leerinhoud详细信息'之外,几乎所有网页都会获得漂亮的网址。出于某种原因,它返回来自Leerinhoud'的内容。但是在一个漂亮的网址下(http://placeholder.com/OC/hoig/leerinhoud/1/the-slug)。
另外,我对[R = 302,L]标志不太确定,因为这是一个临时重定向,我真的不需要。
目标
http://placeholder.com/OC/hoig/index.php至http://placeholder.com/OC/hoig/
http://placeholder.com/OC/hoig/leerinhoud.php到http://placeholder.com/OC/hoig/leerinhoud
http://placeholder.com/OC/hoig/leerinhoud-detail.php?id=1&leerinhoud=the-slug到http://placeholder.com/OC/hoig/leerinhoud/1/the-slug
...
htaccess的:
RewriteEngine On
RewriteBase /OC/hoig/
RewriteCond %{THE_REQUEST} /news-detail\.php\?id=([^\s&]+)&news=([^\s&]+) [NC]
RewriteRule ^ nieuws/%1/%2? [R=302,L]
RewriteRule ^nieuws/([^/]+)/([^/]+)/?$ news-detail.php?id=$1&news=$2 [L,NC,QSA]
RewriteCond %{THE_REQUEST} /leerinhoud\.php [NC]
RewriteRule ^ leerinhoud [R=302,L]
RewriteRule leerinhoud leerinhoud.php [L,NC,QSA]
RewriteCond %{THE_REQUEST} /leerinhoud-detail\.php\?id=([^\s&]+)&leerinhoud=([^\s&]+) [NC]
RewriteRule ^ leerinhoud/%1/%2? [R=302,L]
RewriteRule ^leerinhoud/([^/]+)/([^/]+)/?$ leerinhoud-detail.php?id=$1&leerinhoud=$2 [L,NC,QSA]
RewriteCond %{THE_REQUEST} /partners\.php [NC]
RewriteRule ^ partners [R=302,L]
RewriteRule partners partners.php [L,NC,QSA]
RewriteCond %{THE_REQUEST} /contact\.php [NC]
RewriteRule ^ contact [R=302,L]
RewriteRule contact contact.php [L,NC,QSA]
# RewriteCond %{THE_REQUEST} /search\.php\?q=([^\s&]+) [NC]
# RewriteRule ^ search-results/%1? [R=302,L]
# RewriteRule search-results/([^/]+)/?$ search.php?q=$1 [L,NC,QSA]
RewriteCond %{THE_REQUEST} /rss-detail\.php\?rss=([^\s&]+) [NC]
RewriteRule ^ rss/%1? [R=302,L]
RewriteRule rss/([^/]+)/?$ rss-detail.php?rss=$1 [L,NC,QSA]
RewriteCond %{THE_REQUEST} /tags\.php [NC]
RewriteRule ^ tags [R=302,L]
RewriteRule tags tags.php [L,NC,QSA]
RewriteCond %{THE_REQUEST} /tag\.php\?tag=([^\s&]+) [NC]
RewriteRule ^ tag/%1? [R=302,L]
RewriteRule tag/([^/]+)/?$ tag.php?tag=$1 [L,NC,QSA]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^ nieuws [R=302,L]
RewriteRule nieuws index.php [L,NC,QSA]
TL; DR:如何为我的所有网页获取一些简洁的SEO友好网址?
提前致谢。
答案 0 :(得分:1)
你有2个解决方案:
首先是交换规则订单
RewriteCond %{THE_REQUEST} /leerinhoud-detail\.php\?id=([^\s&]+)&leerinhoud=([^\s&]+) [NC]
RewriteRule ^ leerinhoud/%1/%2? [R=302,L]
RewriteRule ^leerinhoud/([^/]+)/([^/]+)/?$ leerinhoud-detail.php?id=$1&leerinhoud=$2 [L,NC,QSA]
RewriteCond %{THE_REQUEST} /leerinhoud\.php [NC]
RewriteRule ^ leerinhoud [R=302,L]
RewriteRule leerinhoud leerinhoud.php [L,NC,QSA]
其次是为leerinhoud
设置模式的开始/结束(这样,规则的顺序就不再重要了)
RewriteCond %{THE_REQUEST} /leerinhoud\.php [NC]
RewriteRule ^ leerinhoud [R=302,L]
RewriteRule ^leerinhoud$ leerinhoud.php [L,NC,QSA]
RewriteCond %{THE_REQUEST} /leerinhoud-detail\.php\?id=([^\s&]+)&leerinhoud=([^\s&]+) [NC]
RewriteRule ^ leerinhoud/%1/%2? [R=302,L]
RewriteRule ^leerinhoud/([^/]+)/([^/]+)/?$ leerinhoud-detail.php?id=$1&leerinhoud=$2 [L,NC,QSA]
<强>结论强>
了解RewriteRule leerinhoud leerinhoud.php [L,NC,QSA]
和RewriteRule ^leerinhoud$ leerinhoud.php [L,NC,QSA]
不一样。第一个将始终匹配,因为leerinhoud
可以位于您的网址中的任何位置(例如leerinhoud.php
或leerinhoud/something/slug
)。
注意:当您重写没有扩展名的文件名
时,您可能还必须禁用MultiViews
选项
Options -MultiViews