简体   繁体   English

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

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

I hope so you could help me solve this issue. 希望您能帮助我解决此问题。 I am trying to setup my apache server redirects, but without success. 我正在尝试设置我的apache服务器重定向,但是没有成功。 I am trying to do this at at once: 我试图立即这样做:

  1. http://www.example.com -> redirects to -> https:/ /example.com http://www.example.com - >重定向到 - > https:/ / example.com
  2. http://example.com -> redirect to -> https:/ /example.com http://example.com - >重定向到 - > https:/ / example.com
  3. https://www.example.com -> redirect to -> https://example.com https://www.example.com - >重定向到 - > https://example.com
  4. (if the URL contains file extensions like .php/.html I want to remove them) LIKE: https:/ /example.com/portfolio.php -> to be replaced to -> https://example.com/portfolio (如果URL包含文件扩展名,如.php / .html我想删除它们)LIKE:https://example.com/portfolio.php - >要替换为 - > https://example.com/portfolio
  5. URLs can also contain 1 or 2 URL params and I want to replace the url separated by / like that: https://example.com/portfolio.php?id=1&t=Max -> to be replaced to -> https://example.com/portfolio/1/Max 网址还可以包含1个或2个网址参数,我想用以下内容替换/分隔的网址: https//example.com/portfolio.php?id = 1&t = Max - >替换为 - > https:/ /example.com/portfolio/1/Max

Also all redirects should use R=301 if possible. 如果可能,所有重定向也应使用R = 301。

Here is what I have so far, but it's not working. 这是我到目前为止所做的,但它不起作用。

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]

Please help me solve this issue. 请帮我解决这个问题。 It took me so much reading but never did it right. 我花了很多时间阅读,但从未做过正确的事。 The main problem is that Google Crawlers recognize my pages as duplicated, and I have canonical URL problems which I want to resolve. 主要问题是Google Crawlers将我的网页识别为重复,并且我有我想要解决的规范网址问题。

Thank you for the help! 感谢您的帮助!

Not perfect, but conceptually, this will take care of part of it. 不完美,但从概念上讲,这将照顾其中的一部分。 Redirect http to https, solving 1, 2, & 3. 将http重定向到https,解决1,2和3。

File found in /etc/apache2/sites-enabled on Linux 在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>

There are a lot of things to consider, depending on your configuration. 根据您的配置,有很多事情需要考虑。

You'll need to restart Apache after modifying the file. 修改文件后,您需要重新启动Apache。

Try this : 尝试这个 :

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]

The first three lines after RewriteEngine on is to make sure all requests are https://example.... RewriteEngine on后的前三行是确保所有请求均为https://example....

The next two lines is to remove both html & php extentions. 接下来的两行是删除html & php扩展。

The next two line capture any query string /portfolio?id=1&t=max and rewrite it to /portfolio/1/Max externally. 接下来的两行捕获任何查询字符串/portfolio?id=1&t=max ,然后从外部将其重写为/portfolio/1/Max

The next two line capture any URI like /portfolio/1/Max and rewrite it to original path internally. 接下来的两行捕获任何URI,例如/portfolio/1/Max并将其内部重写为原始路径。

The rest of lines are to redirect the rest of URIs and check whether they are .php or .html internally. 其余各行用于重定向其余URI,并在内部检查它们是.php or .html

Note: clear browser cache and then test 注意:清除浏览器缓存然后测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM