[英]Redirect/rewrite with htaccess
我剛剛開始學習.htaccess,並且當當前URL中不包含某些內容和/或看起來不像時,我似乎無法理解如何重定向到該URL。
例如:
有人試圖訪問www.example.com/contact.php。 但是我的htaccess表示,每個非www.example.com或不包含'?page ='(www.example.com/index.php?page=contact.php)的網址,都應更改為www.example .com。
同樣,此檢查必須在此之前:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
因此,只要用戶輸入example.com,它將被重定向到www.example.com。 如果它寫example.com/contact.php或www.example.com/contact.php,它將被重定向到www.example.com,因為它不是www.example.com,也不包含'?page ='。
我認為這段代碼將解決您的問題,並幫助您了解htaccess規則的一些知識:
# Active rewrite engine
RewriteEngine On
# Force www.
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
# If it's the homepage
RewriteCond %{REQUEST_URI} ^/?$
# Skip the next 4 rules (self-inclusion)
RewriteRule .* - [S=4]
# If url is not "index.php"
RewriteCond %{REQUEST_URI} !^/index.php [OR]
# Or does not contain "page="
RewriteCond %{QUERY_STRING} !(^|&)page=.+(&|$)
# Redirect to homepage
RewriteRule . /? [L,R=301]
您會嘗試嗎,通過htaccess將動態鏈接轉換為搜索引擎可讀鏈接的網站真棒, 轉換為htaccess文件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.