簡體   English   中英

htaccess規范URL和HTTP-> HTTPS重定向(自定義URL)

[英]htaccess Canonical URL and HTTP->HTTPS redirects (custom url)

希望您能幫助我解決此問題。 我正在嘗試設置我的apache服務器重定向,但是沒有成功。 我試圖立即這樣做:

  1. http://www.example.com - >重定向到 - > https:/ / example.com
  2. http://example.com - >重定向到 - > https:/ / example.com
  3. https://www.example.com - >重定向到 - > https://example.com
  4. (如果URL包含文件擴展名,如.php / .html我想刪除它們)LIKE:https://example.com/portfolio.php - >要替換為 - > https://example.com/portfolio
  5. 網址還可以包含1個或2個網址參數,我想用以下內容替換/分隔的網址: https//example.com/portfolio.php?id = 1&t = Max - >替換為 - > https:/ /example.com/portfolio/1/Max

如果可能,所有重定向也應使用R = 301。

這是我到目前為止所做的,但它不起作用。

RewriteEngine On

RewriteCond %{SERVER_PORT} 80

RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

RewriteRule ^portfolio/(\d+)/(.*) portfolio.php?id=$1 [QSA,L] 

RewriteRule ^portfolio portfolio.php [QSA,L]

RewriteRule ^about about.php [QSA,L]

請幫我解決這個問題。 我花了很多時間閱讀,但從未做過正確的事。 主要問題是Google Crawlers將我的網頁識別為重復,並且我有我想要解決的規范網址問題。

感謝您的幫助!

不完美,但從概念上講,這將照顧其中的一部分。 將http重定向到https,解決1,2和3。

在Linux上的/ etc / apache2 / sites-enabled中找到的文件

Listen 443
Listen 80

IfModule mod_ssl.c>
<VirtualHost *:80>
ServerName example.com
redirect / https://example.com
</VirtualHost>

<VirtualHost*:80>
ServerName www.example.com
redirect / https://example.com
</VirtualHost>

<VirtualHost*:443>
ServerName www.example.com
redirect / https://example.com
</VirtualHost>

<VirtualHost *:443>
ServerName example.com

**Put all the other stuff you need here**

</VirtualHost>

根據您的配置,有很多事情需要考慮。

修改文件后,您需要重新啟動Apache。

嘗試這個 :

RewriteEngine on 

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^  https://example.com%{REQUEST_URI} [L,R=301]

RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(php|html)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=301,L]

RewriteCond %{QUERY_STRING} ^id=(\d+)&t=(.+)
RewriteRule ^(.*)$ /$1/%1/%2 [L,R=301]


RewriteCond %{REQUEST_FILENAME}   !-f 
RewriteRule ^([^\/]+)/(\d+)/(.*)$ /$1.php?id=$2&t=$3 [L]

RewriteCond %{REQUEST_FILENAME}   !-f 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*) /$1.php [L]
RewriteCond %{REQUEST_FILENAME}   !-f 
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*) /$1.html [L]

RewriteEngine on后的前三行是確保所有請求均為https://example....

接下來的兩行是刪除html & php擴展。

接下來的兩行捕獲任何查詢字符串/portfolio?id=1&t=max ,然后從外部將其重寫為/portfolio/1/Max

接下來的兩行捕獲任何URI,例如/portfolio/1/Max並將其內部重寫為原始路徑。

其余各行用於重定向其余URI,並在內部檢查它們是.php or .html

注意:清除瀏覽器緩存然后測試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM