2014-09-19 21 views
1

.htaccess文件真的不是我的東西。他們仍然是一個謎,我可能會永遠...如何重構我的.htaccess以獲得所有我的網頁的語義URL

無論如何,所以我有一個.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.phphttp://placeholder.com/OC/hoig/ http://placeholder.com/OC/hoig/leerinhoud.phphttp://placeholder.com/OC/hoig/leerinhoud http://placeholder.com/OC/hoig/leerinhoud-detail.php?id=1&leerinhoud=the-slughttp://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?

在此先感謝。

回答

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] 

結論
Understan d表示RewriteRule leerinhoud leerinhoud.php [L,NC,QSA]RewriteRule ^leerinhoud$ leerinhoud.php [L,NC,QSA]不一樣。第一個將永遠匹配,因爲leerinhoud可以在您的url中的任何地方(如leerinhoud.phpleerinhoud/something/slug)。

注意:您可能還需要禁用MultiViews選項,當你重寫文件名不帶擴展

Options -MultiViews 
+0

輝煌!正是我需要的答案。但是,你可以解釋一下'MultiViews'行嗎?另外,'R = 302'怎麼樣,這是個好主意嗎? – 2014-09-19 10:35:50

+0

'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

+1

偉大的,感謝賈斯汀這個完美的答案,我可能會學到一些東西:) – 2014-09-19 16:33:01

相關問題