Arif
Arif

Reputation: 1211

.htaccess url rewrite for sub-domain

I have a page in sub-domain domain.com/blog/landing-page/.

I want to open it as domain.com/ so user will not know where my page is located.

I try below code but it doesn't work for me,

RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+blog/landing-page[/\s] [NC]
RewriteRule ^ / [R=301,L]

I wrote this but it open domain/blog

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+blog/landing-page/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^dirname/)^(.*)$ /blog/landing-page/$1 [L,NC]

How can i make it work ?

Upvotes: 2

Views: 323

Answers (3)

anubhava
anubhava

Reputation: 785098

This should work in root .htaccess:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+blog/landing-page[/\s?] [NC]
RewriteRule ^ /? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!blog/landing-page/).*)$ /blog/landing-page/$1 [L,NC]

Upvotes: 1

Sam Miller
Sam Miller

Reputation: 166

This should work

RewriteEngine  On
RewriteBase /

RewriteCond %{REQUEST_URI} blog/landing-page/?$ [NC]
RewriteRule .* / [R=302,L]

Likely you were having an issue with you Regex, but it's good practive to confirm your RewriteBase as well. I left the R=302 so that you can test everything out before you commit to the more permanent R=301

Upvotes: 1

Related Questions