skip to Main Content

I have a specific problem with my mod_rewrite configuration that I cannot resolve. I am no admin, therefore I’m kindly asking for a collective advice 🙂 Please note – it’s not a general question about redirection, but very specific one.

Story

I have a shared hosting with access to FTP and ability to create my own .htaccess files. This shared hosting had plenty of files and directories before I created the website, so logical step for me was to place everything inside new-site folder.

Then I had to create custom rewrite rules so that everything under example.com points to new-site.

CONFIG

So I came up with the following config.

# (...) other rules

# 1. Make sure that /new-site/ is not a duplicated content
RewriteCond %{REQUEST_URI} ^/new-site/
RewriteRule ^/new-site/(.*)$ /$1  [R=301,L]

# 2. Make sure that example.com is internally handled by files in '/new-site'
RewriteCond %{REQUEST_URI} !^/new-site/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /new-site/$1

RESULTS

Rule marked with 2. works fine, my site is accessible as I want. However I didn’t want https://example.com/new-site/ to be found on the server by bots and treated by a duplicated content, so I added rule 1..

This rule, however, doesn’t seem to have any effect! I looked it up with CURL and request is handled immediately with a 200 status. I’m banging my head against the wall and experimenting with other variants of it, but everything fails.

What I’m after is pretty darn simple:

  1. Make every request to the root domain be handled by website which is stored in /new-site/
  2. Make sure that direct call to https://example.com/new-site/(.*) is redirected with 301 status back to the domain root.

What am I doing wrong?

EDIT

I’ve noticed that my setup seems to be doing far better if I remove a child .htaccess file under /new-site/ subfolder. I didn’t mention it in my original question because there is nothing special about it (just some SEO rewrites).

RewriteEngine on

DirectoryIndex index.php

RewriteRule ^products$ products.php
# (...) similar rewrites

2

Answers


  1. Old answer: RewriteRule does not accept leading slash. Try to change to

    RewriteRule ^new-site/(.*)$ /$1  [R=301,L]
    

    Edit:
    Version that is provided by you will forward to the cyclic redirection. To avoid it, I think, you can use such .htaccess

    RewriteEngine on
    RewriteRule ^new-site/ - [R=404,L]
    
    # 2. Make sure that example.com is internally handled by files in '/new-site'
    RewriteCond %{REQUEST_URI} !^/new-site/
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /new-site/$1 [L]
    

    Direct asking /new-site/* wil receive 404 error, while url exaple.com/* wil be redirected to /new-site
    And notice that if there are files with the same name, for example, /r.jpg and /new-site/r.jpg, the last never be achieve

    Login or Signup to reply.
    1. Your first rule never matches because it must not begin with a leading slash.

      With RewriteRule, you only need a leading slash if you’re directly in httpd.conf or before Apache v2.4 i think.

    2. While you have a good idea, your first rule will cause an infinite redirection loop if it’s working. You have to use THE_REQUEST to match direct user request only.

    You can put this code in /.htaccess

    # 1. Make sure that /new-site/ is not a duplicated content
    RewriteCond %{THE_REQUEST} s/new-site/([^s]*)s [NC]
    RewriteRule ^ /%1 [R=301,L]
    
    # 2. Make sure that example.com is internally handled by files in '/new-site'
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^((?!new-site/).*)$ /new-site/$1 [L]
    

    Also, you’ll have to add this line in /new-site/.htaccess (to avoid automatic override)

    RewriteOptions InheritBefore

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search