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:
- Make every request to the root domain be handled by website which is stored in
/new-site/
- Make sure that direct call to
https://example.com/new-site/(.*)
is redirected with301
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
Old answer: RewriteRule does not accept leading slash. Try to change to
Edit:
Version that is provided by you will forward to the cyclic redirection. To avoid it, I think, you can use such .htaccess
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
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 inhttpd.conf
or before Apache v2.4 i think.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
Also, you’ll have to add this line in
/new-site/.htaccess
(to avoid automatic override)RewriteOptions InheritBefore