Defyleiti
Defyleiti

Reputation: 555

Non https main site redirects to https with directory

I'm using this piece of code to redirect all http to https in my .htaccess file.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Works great. But my only problem is if I access the root non http

http://example.com redirects to https://www.example.com/dev/

/dev is the directory where the main site is located.

my cpanel directory is like this

public_html/dev <---- https://www.example.com

How can I fix this issue. It should redirect to https version without the dev directory. Hope someone could shed a light on this. thanks.

EDIT: To make it more clear here's the directory structure enter image description here

The contents of .htaccess in public_html is

public_html/.htaccess

rewritecond %{HTTP_HOST} ^example.com$ [NC,OR]
rewritecond %{HTTP_HOST} ^www.example.com$
rewritecond %{REQUEST_URI} !dev/
rewriterule (.*) /dev/$1 [L]

RewriteCond %{HTTP_HOST} ^oldurl\.com$ [OR] // if old url redirect to example.com
RewriteCond %{HTTP_HOST} ^www\.oldurl\.com$ // if old url redirect to example.com
RewriteRule ^/?$ "https\:\/\/www\.example\.com" [R=301,L]

here's the contents of .htaccess file in public_html/dev

public_html/dev/.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

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

Upvotes: 1

Views: 51

Answers (1)

anubhava
anubhava

Reputation: 785266

Have it this way.

public_html/.htaccess

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

# enforce HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# First add a trailing slash if dev/$1 is a directory
RewriteCond %{DOCUMENT_ROOT}/dev/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=301,NE]

# forward everything to /dev directory
RewriteCond %{REQUEST_URI} !^/dev/ [NC]
RewriteRule .* dev/$0 [L]

public_html/dev/.htaccess:

RewriteEngine On
RewriteBase /dev/

RewriteRule ^index\.php$ - [L,NC]

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

Make sure to clear your browser cache completely before testing this changge.

Upvotes: 1

Related Questions