繁体   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