简体   繁体   中英

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. I am trying to do this at at once:

  1. http://www.example.com -> redirects to -> https:/ /example.com
  2. http://example.com -> redirect to -> https:/ /example.com
  3. https://www.example.com -> redirect to -> 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
  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

Also all redirects should use R=301 if possible.

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.

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.

File found in /etc/apache2/sites-enabled on Linux

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.

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

The next two lines is to remove both html & php extentions.

The next two line capture any query string /portfolio?id=1&t=max and rewrite it to /portfolio/1/Max externally.

The next two line capture any URI like /portfolio/1/Max and rewrite it to original path internally.

The rest of lines are to redirect the rest of URIs and check whether they are .php or .html internally.

Note: clear browser cache and then test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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