skip to Main Content

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

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search