htaccess规范URL和HTTP-> HTTPS重定向(自定义URL)

问题描述 投票:0回答:2

我希望你能帮助我解决这个问题。我正在尝试设置我的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将我的网页识别为重复,并且我有我想要解决的规范网址问题。

感谢您的帮助!

.htaccess redirect canonical-link
2个回答
0
投票

不完美,但从概念上讲,这将照顾其中的一部分。将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。


0
投票

试试这个 :

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....

接下来的两行是删除两个qazxsw poi扩展。

接下来的两行捕获任何查询字符串html & php并从外部将其重写为/portfolio?id=1&t=max

接下来的两行捕获任何URI,如/portfolio/1/Max,并在内部将其重写为原始路径。

其余行将重定向其余的URI并检查它们是否在内部是/portfolio/1/Max

注意:清除浏览器缓存然后测试

© www.soinside.com 2019 - 2024. All rights reserved.