首页 > 解决方案 > htaccess 将 example.com/about 重定向到 https://www.example.com/about

问题描述

以下代码将许多请求重定向到https://www.example.com,但请求 example.com/about 会重定向到https://www.example.com。如何使其重定向到请求的页面(https://www.example.com/about)?

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

    RewriteCond %{HTTP:Authorization} ^(.+)$
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

    RewriteCond %{HTTPS} !^on$
    RewriteRule (.*) https://www.example.com/$1 [R,L]

    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

    <Files "*.txt">
      Require all denied
    </Files>
</IfModule>

标签: .htaccess

解决方案


It seems that you want to redirect all unavailable variation to home page, rather to relevant page or 404 page.

If it's so, then I must say it's best practice to have 404 page instead redirecting to home page.

Otherwise you can use below code for your purpose, just add below lines in your code and remove other redirections.

ErrorDocument 404 http://domainname.com

推荐阅读