2017-01-27 66 views
1

我有一個website與11個子domains.I具有一個主htaccess文件中的public_html根文件夾具有共同共享性狀等cache等我在每個子域的單獨htaccess文件我使用的根目錄文件夾mod_rewrite但是我無法重寫URL。我試圖將其從dundaah.com/days/content.php?day=thur更改爲更清潔的dundaah.com/thursday,並將子域music.dundaah.com/days/content.php?day=thur更改爲首選music.dundaah.com/thursday。到目前爲止,我的第一個研究代碼只能讓我訪問URL dundaah.com/day/thur,第二個到dundaah.com/thursday仍然給我一個404錯誤。我沒有改變網站的內部鏈接,這是一個實體嗎?誰能幫忙?由於mod_rewrite的不工作的htaccess

<IfModule mod_rewrite.c> 
 

 
    Options +FollowSymlinks 
 
    
 
    # enable the rewrite engine 
 
    RewriteEngine On 
 
    RewriteCond %{REQUEST_URI} ^/days/content.php 
 
    RewriteCond %{QUERY_STRING} ^day=([a-z]+)$ 
 
    RewriteRule ^(.*)$ /days/%1? [R,L] 
 

 
    # or for individual pages 
 
    RewriteCond %{REQUEST_URI} ^/days/content.php 
 
    RewriteCond %{QUERY_STRING} ^day=thur$ 
 
    RewriteRule ^(.*)$ /thursday? [R,L] 
 
</IfModule>

+0

如果您直接在瀏覽器地址欄中輸入url example.com/thursday,是否可以正常工作? – starkeen

+0

使用上面的代碼時沒有不需要 –

+0

每天都需要一個不同的規則或者你可以使用'RewriteMap' – anubhava

回答

0

我的歉意,我是一個在我的網站使用壞文件夾目錄系統。這是一個用

public_html(www directory) -index.php -vidz(sub-domain) -index.php -days -_imports -css -js -php -content.php -contact.php

這是我升級後的系統

public_html(www directory) -index.php -vidz(sub-domain) -index.php -content.php -contact.php -assets -_imports -css -js -php

這使得它有聯繫的<a href='monday'>Mon</a>和使用.htaccess重定向以下規則可能。

<IfModule mod_rewrite.c> 
 

 
    Options +FollowSymlinks 
 
    RewriteEngine On 
 
    RewriteRule ^monday$ content.php?day=mon [NC,L] 
 
    RewriteRule ^tuesday$ content.php?day=tue [NC,L] 
 
    RewriteRule ^wednesday$ content.php?day=wed [NC,L] 
 
    RewriteRule ^thursday$ content.php?day=thur [NC,L] 
 
    RewriteRule ^friday$ content.php?day=fri [NC,L] 
 
    RewriteRule ^saturday$ content.php?day=sat [NC,L] 
 
    RewriteRule ^sunday$ content.php?day=sun [NC,L] 
 
    RewriteRule ^contact$ contact.php [NC,L] 
 

 
</IfModule>

感謝所有誰回答!它幫助我意識到這一點。

1

兩個Apachenginx內部外部重定向的概念。

你可以做什麼來完成你的任務是創建一個「重定向循環」,其中/days/content.php?day=thurredirect/R(外部重定向)到/thursday,而/thursday將內部回去/days/content.php?day=thur,這可能最初看起來像一個循環。

參見:

這可以通過測試在每種情況下完成對請求URI

RewriteCond %{REQUEST_URI} ^/days/content.php$ 
RewriteCond %{QUERY_STRING} ^day=thur$ 
RewriteRule ^(.*)$ /thursday? [R,L] # external redirect 

RewriteCond %{REQUEST_URI} ^/thursday$ 
RewriteRule ^(.*)$ /days/content.php?day=thur [L] # internal redirect 
+0

這很好,瀏覽器中的網址顯示'dundaah.com/thursday',但不顯示內容只是' dundaah.com重定向你太多次了,我應該嘗試清除我的cookies,但即使我做了,仍然得到了相同的信息。內部鏈接對於索引頁面仍然保留'days/content.php?day = thur','days.'文件夾內的文件只有兩個'content.php&contact。'content.php?day = thur'。 php'。我沒有任何刪除'.php'擴展的代碼。 –

+0

@MaxNjoroge,你確定只有一個'R'嗎?這聽起來像你正在創建一個實際的循環,在每種情況下都有外部重定向,而不是在另一個內部有重定向。 – cnst

+0

您不能使用「REQUEST_URI」,因爲這是通過重寫更新的。你可以使用的是「THE_REQUEST」,這是原始請求,不會被修改。或者就像我在回答中一樣使用[END],所以在成功匹配之後,mod_rewrite將不再執行任何處理。 – SuperDuperApps

2

這將使/thursday工作(和所有其他日子),把它放在你的主.htaccess和你的su b域的。它還會將您的舊網址重定向到新網址。

它只會在Apache 2.4或更高版本上運行。讓我知道如果你不是,我會更新答案。

RewriteEngine on 

# Redirect old URLs 
RewriteCond %{QUERY_STRING} =day=mon 
RewriteRule ^days/content\.php$ /monday [R=301,END] 
RewriteCond %{QUERY_STRING} =day=tue 
RewriteRule ^days/content\.php$ /tuesday [R=301,END] 
RewriteCond %{QUERY_STRING} =day=wed 
RewriteRule ^days/content\.php$ /wednesday [R=301,END] 
RewriteCond %{QUERY_STRING} =day=thur 
RewriteRule ^days/content\.php$ /thursday [R=301,END] 
RewriteCond %{QUERY_STRING} =day=fri 
RewriteRule ^days/content\.php$ /friday [R=301,END] 
RewriteCond %{QUERY_STRING} =day=sat 
RewriteRule ^days/content\.php$ /saturday [R=301,END] 
RewriteCond %{QUERY_STRING} =day=sun 
RewriteRule ^days/content\.php$ /sunday [R=301,END] 

# Rewrite new URLs 
RewriteRule ^monday$ days/content.php?day=mon [END] 
RewriteRule ^tuesday$ days/content.php?day=tue [END] 
RewriteRule ^wednesday$ days/content.php?day=wed [END] 
RewriteRule ^thursday$ days/content.php?day=thur [END] 
RewriteRule ^friday$ days/content.php?day=fri [END] 
RewriteRule ^saturday$ days/content.php?day=sat [END] 
RewriteRule ^sunday$ days/content.php?day=sun [END] 

由於腳本使用簡短的表單而新的URL沒有,所以最好單獨做這些日子。如果需要,您可以更改它們,或者如果我錯過了某些內容,請告訴我。