简体   繁体   中英

Permanently (301) redirect all urls to homepage with htaccess

I have a website with lots of page and I want all pages/urls will be permanently redirected (301) to homepage by htaccess. Can we please let me how could I achieve this?

I want something like that.

mywebsite.com to https://www.mywebsite.com/

mywebsite.com/page to https://www.mywebsite.com/

mywebsite.com/assets/img.jpg to https://www.mywebsite.com/

Here is my current htaccess lines

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Thank you

Converting my comment to answer so that solution is easy to find for future visitors.

You may use this single rule:

RewriteEngine On

# redirect to landing page if it is
# non-www or non-https or any URL other than landing page
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{REQUEST_URI} ^/.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule . https://www.%1/ [R=301,L]

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