itshimelkazi
itshimelkazi

Reputation: 71

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

Upvotes: 1

Views: 62

Answers (1)

anubhava
anubhava

Reputation: 786091

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]

Upvotes: 2

Related Questions