jonathan
jonathan

Reputation: 114

Redirect documentroot to another folder in conf apache or .htccess

Good Day

I want to rewrite a url when request a subdirectory When the user request

172.0.0.1/url

i want to rewrite to make this url point to

172.0.0.1/url/url

or the documentroot is in /var/www/html/url/url make this point to

172.0.0.1/url

Upvotes: 2

Views: 92

Answers (2)

Justin Iurman
Justin Iurman

Reputation: 19016

The trick here is to avoid creating a rewrite and a redirect loop. You can put this code in your root folder htaccess

RewriteEngine On

# Redirect /url/url/xxx to /url/xxx
RewriteCond %{THE_REQUEST} \s/(url)/url/(.*)\s [NC]
RewriteRule ^ /%1/%2 [R=301,L]

# Internally rewrite back /url/xxx to /url/url/xxx
RewriteRule ^(url)/((?!url/).*)$ /$1/$1/$2 [L]

Upvotes: 3

SiddheshNan
SiddheshNan

Reputation: 90

You can use .htaccess to redirect /url to /url/url

Just Create .htaccess file at /var/www/html/ with following contains:

RewriteEngine on
RewriteRule ^/?url/(.*)$ /url/url/$1 [R,L]

This will Redirect all incoming requests at /url and redirect them to /url/url

But you cannot redirect requests coming at /url/url to /url Because we are already redirecting request at /url, this will result in catch 22 and browser will keep redirecting back and forth between /url and /url/url.

Upvotes: 0

Related Questions