.htaccess文件真的不是我的东西。他们仍然是一个谜,我可能会永远...如何重构我的.htaccess以获得所有我的网页的语义URL
无论如何,所以我有一个.htaccess文件与所有不同的页面相当一些重写规则。
网址:http://placeholder.com/OC/hoig/[page]
文件夹结构:
- 指数(新闻):http://placeholder.com/OC/hoig/index.php
- 合作伙伴:http://placeholder.com/OC/hoig/partners.php
- 联系人:http://placeholder.com/OC/hoig/contact.php
- Leerinhoud(是的,这是荷兰):http://placeholder.com/OC/hoig/leerinhoud.php
- 搜索:http://placeholder.com/OC/hoig/search.php?q=test
的目标是让语义(相当)所有页面的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:如何为我的所有页面获得一些kickass clean SEO友好的URL?
在此先感谢。
辉煌!正是我需要的答案。但是,你可以解释一下'MultiViews'行吗?另外,'R = 302'怎么样,这是个好主意吗? – 2014-09-19 10:35:50
'R = 302'是一个临时重定向。现在您的代码正在工作,您的情况下永久重定向会更好(使用'R = 301'代替)。关于'MultiViews',这是一个用于内容关联的Apache选项。如果您请求不存在的'/ foo',并且您有一个名为'foo.html'的文件,它将自动提供'foo.html'(如果启用了MultiViews)。这就是为什么禁用它会更好(因为你的规则使用相同的文件名而没有扩展名)。更多的信息在这里:http://httpd.apache.org/docs/2.0/en/content-negotiation.html – 2014-09-19 11:35:40
伟大的,感谢贾斯汀这个完美的答案,我可能会学到一些东西:) – 2014-09-19 16:33:01