Marios Fakiolas
Marios Fakiolas

Reputation: 1545

HTACCESS Redirection from www.url.com to www.url.com/home

I am using following htaccess file to achieve some redirections etc:

RewriteEngine On

Options +FollowSymLinks 
RewriteEngine on

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

RewriteCond %{HTTPS} !=on
RewriteRule .* https://www.myurl.com/$1 [R,L]

RewriteRule ^$ /home [R=301,L]

Lately i ve added the latest line so when user visits https://www.myurl.com he gets redirected to https://www.myurl.com/home for the homepage. This works fine as wanted but i have a bad feeling that this should be written somehow better. I don't like the ^$ part but it works. How could i rewrite this?

Upvotes: 0

Views: 128

Answers (1)

AbsoluteƵERØ
AbsoluteƵERØ

Reputation: 7880

The HTTPS part can definitely be rewritten and should come first as you're creating multiple redirects.

RewriteEngine On

Options +FollowSymlinks 
RewriteBase /

#redirect all traffic looking for a domain not starting with www &
#all non-https traffic to the https://www domain
RewriteCond %{HTTPS} off
#This condition is better for analytics tracking and SEO (single subdns)
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteRule ^index\.php$ - [L]

RewriteRule ^$ home [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Since your MVC is likely using index.php to process all requests it's likely that the redirect to /home will not work unless you're visiting the website without requesting any file (the root only). If someone explicitly requests https://www.myurl.com/index.php your rule that says RewriteRule ^index\.php$ - [L] tells the site to not change anything. Since you said this is working as expected I've not changed the order.

If it doesn't work as intended, then switch the order of the rule for /home.

RewriteRule ^$ home [R=301,L]
RewriteRule ^index\.php$ - [L]

Upvotes: 1

Related Questions