2015-05-01 71 views
1

我在使用htaccess重定向時遇到問題。我們有一個新的網站,我們希望不再使用網址中的擴展名。我們一直在使用.php和.html擴展名。我也想將所有頁面重定向到www的版本。前面。我們還首次使用https,因此所有內容也應該重定向到https://www。當然,所有與301重定向搜索引擎優化。一些舊文件在文件名中有下劃線,我想將它們轉換爲破折號。我現在正在使用這個艙口蓋,但它工作不正確。使用mod重寫在htaccess中重定向的問題

# activeer rewrite engine 
RewriteEngine On 

# bepaal je rootdirectory (set root) 
RewriteBase/

# verwijder de .php extensie (remove .php) 
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP 
RewriteRule (.*)\.php$ $1 [R=301] 

# verwijder de .html extensie (remove .html) 
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP 
RewriteRule (.*)\.html$ $1 [R=301] 

# verwijder index (delete index) 
RewriteRule (.*)/index$ $1/ [R=301] 

# verwijder de trailing slash als het geen map is (remove trailingtrash) 
# RewriteCond %{REQUEST_FILENAME} !-d 
# RewriteCond %{REQUEST_URI} /$ 
# RewriteRule (.*)/ $1 [R=301] 

# verzoek om te verwijzen naar php bestand (request php-file) 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteCond %{REQUEST_URI} !/$ 
RewriteRule (.*) $1\.php [L] 

# verzoek om te verwijzen naar html bestand (request php-file) 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteCond %{REQUEST_URI} !/$ 
RewriteRule (.*) $1\.html [L] 

# underscores omzetten in streepjes (convert underscores to dashes) 
RewriteCond %{REQUEST_URI} ^(.*)_(.*)$ 
RewriteRule (.*)_(.*)/ http://www.previewwebsite.nl$1-$2/ [R=301] 

# zonder www verwijzen naar www (redirect to www. instead of non-www) 
RewriteCond %{HTTP_HOST} ^previewwebsite\.nl [NC] 
RewriteRule ^(.*)$ http://www.previewwebsite.nl/$1 [L,R=301] 

你可以在http://www.previewwebsite.nl/kasteel_trouwen.php上測試它。我沒有在此域上設置SSL,僅用於測試目的。

回答

1

嘗試在重定向中添加L標誌,否則其後的規則將被應用。此外,請確保您的所有重定向都位於頂端:

# activeer rewrite engine 
RewriteEngine On 

# bepaal je rootdirectory (set root) 
RewriteBase/

# underscores omzetten in streepjes (convert underscores to dashes) 
RewriteCond %{REQUEST_URI} ^(.*)_(.*)$ 
RewriteRule (.*)_(.*)/ https://www.previewwebsite.nl$1-$2/ [L,R=301] 

# zonder www verwijzen naar www (redirect to www. instead of non-www) 
# also redirect to HTTPS 
RewriteCond %{HTTPS} off [OR] 
RewriteCond %{HTTP_HOST} ^previewwebsite\.nl [NC] 
RewriteRule ^(.*)$ https://www.previewwebsite.nl/$1 [L,R=301] 

# verwijder de .php extensie (remove .php) 
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP 
RewriteRule (.*)\.php$ $1 [L,R=301] 

# verwijder de .html extensie (remove .html) 
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP 
RewriteRule (.*)\.html$ $1 [L,R=301] 

# verwijder index (delete index) 
RewriteRule (.*)/index$ $1/ [L,R=301] 

# verwijder de trailing slash als het geen map is (remove trailingtrash) 
# RewriteCond %{REQUEST_FILENAME} !-d 
# RewriteCond %{REQUEST_URI} /$ 
# RewriteRule (.*)/ $1 [L,R=301] 

# verzoek om te verwijzen naar php bestand (request php-file) 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteCond %{REQUEST_URI} !/$ 
RewriteRule (.*) $1\.php [L] 

# verzoek om te verwijzen naar html bestand (request php-file) 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteCond %{REQUEST_URI} !/$ 
RewriteRule (.*) $1\.html [L] 
+0

非常感謝。不幸的是,它並不適用於所有情況。這是行不通的:http://previewwebsite.nl/faciliteiten.php> http://www.previewwebsite.nl/http://previewwebsite.nl/www/htdocs/testssit/faciliteiten。這也不是:http://www.previewwebsite.nl/kasteel_trouwen.php> http://www.previewwebsite.nlhttp://www.previewwebsite.nl/www/htdocs/testssit/kasteel-trouwen。希望你能幫助... – Farl