2

I'm trying to do a simple invisible redirection with .htaccess which almost work. I'd like my main domain name example.com to access the content of the folder example.com/V8/ but keeping only the domain name in the URL.

A weird thing happens when I use the following code:

DirectoryIndex index.php
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /V8/$1 [NC,L]

This code does redirect to the right folder without changing the URL, but as soon as the page is loaded, another redirection happens to a white page with my email address as a link. When I look at the source code it says:

<a href='mailto:[email protected]'>[email protected]</a>

Then if I directly go to example.com/V8/, I can access the page without being redirected to this email page.

If I update the last line of the .htaccess to:

RewriteRule ^(.*)$ /V8/$1 [R=301,NC,L]

There's no double redirection, but the redirection I want is not invisible anymore because of the R=301.

Can someone help me find out what the problem is?

1
  • That secondary redirect looks like it is being triggered by your application, not Apache? What do you see in the network traffic? What (3xx?) status codes?
    – MrWhite
    Nov 23, 2017 at 15:29

2 Answers 2

1

I did many tests looking at the console and found out that the following line in my index.php was causing me the trouble:

<base href="/V8/">

Once getting rid of this line and updating my internal links and references, everything works the way I want, keeping my original code on my .htaccess

By the way @mrwhite, using your code for the .htaccess caused an unexpected result, it was showing me the content of the root folder with sub-directories as links and list of root files

To prevent from it, I commented this line:

RewriteCond %{REQUEST_FILENAME} !-d

is it normal ?

Bye the way, here is the website: www.sebmuller.com

4
  • "updating my internal links" - You appear to have hardcoded /V8/ in all your "internal links" - that would seem to conflict with removing the /V8 from the page URL? (But this is what the BASE tag would have done anyway?) By including /V8/ you don't need to rewrite all these URLs, which is why you are only rewriting the document root (on your single-page-website). But in doing so you are exposing the /V8 subdirectory to the user. And since your URLs are relative, if you now request example.com/V8/ directly, you get the same page but without CSS/JS.
    – MrWhite
    Nov 24, 2017 at 15:05
  • Yes, the check for the directory (RewriteCond %{REQUEST_FILENAME} !-d) was an error on my part, as it would have prevented the document root from being rewritten. (I've updated my answer.) With that line in place, the directive just wouldn't have done anything - the URL would not have been rewritten - so you were seeing a directory listing (because I guess you don't have an index document in the document root and directory indexes are enabled).
    – MrWhite
    Nov 24, 2017 at 15:08
  • 1
    Hey MrWhite, Thanks a lot for all your info. I finally manage to do what I wanted, thanks to all of your advices. I finally used the last edited version of your code and got rid of all the hardcoded /V8/ to be able to make it work when directly accessed. I also had to have a .htaccess file in every folder that I want to access directly with this line DirectoryIndex index.php. Without it, when I wanted to directly access /V8/ for instance, the url was rewrited to /V8/V8/ Thanks again !
    – Kinder
    Nov 28, 2017 at 13:01
  • You shouldn't need multiple .htaccess files since the DirectoryIndex directive should be inherited from the .htaccess in the parent directory. (Besides, having multiple .htaccess files is something you should try to avoid.) Accessing the /V8 directory directly would indeed be problematic with the above directives (I don't see how the DirectoryIndex directive would resolve this?). You could have added a condition that excludes direct requests for the /Vn directory? But having /Vn accessible potentially creates duplicate content, so is best avoided anyway?
    – MrWhite
    Nov 28, 2017 at 15:19
1

but as soon as the page is loaded, another redirection happens

This sounds as if this redirection is being triggered by your application logic, not .htaccess. Are you using a CMS of some kind?


RewriteCond %{REQUEST_URI} ^/$

However your directive does not look like it does what you intend? Your directive will only rewrite requests for the document root ie. example.com/ to example.com/V8/. From your description (and RewriteRule captured patterns / backreferences) it looks like you want to rewrite example.com/<anything> to example.com/V8/<anything>?

In which case, you would need something like the following instead:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /V8/$1 [L]

This rewrites everything to the /V8 subdirectory. The check against REDIRECT_STATUS prevents a rewrite loop (empty on the first request, but set to 200 after the first successful rewrite). The NC flag is unnecessary, since you are simply matching everything - case does not apply here.


UPDATE: If you have existing files outside of your /V8 directory that still need to be accessible (by direct access) then you can add a couple of conditions:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /V8/$1 [L]

This will prevent any direct requests for existing files (eg. /V2/path/to/file) being rewritten.

2
  • Thanks for your reply @mrwhite. >This sounds as if this redirection is being triggered by your application logic, not .htaccess. Are you using a CMS of some kind? Nop, no CMS at all. I have other subfolders I want to keep access to. If I follow your suggestion, if I type example.com/V2/, that will lead to example.com/V8/V2/ right? That's not what I want. I actually want to rewrite only the root address to the /V8/ folder.
    – Kinder
    Nov 23, 2017 at 16:31
  • Do you have any other .htaccess files? (What about my comment above regarding network traffic and HTTP status codes?) I've updated my answer to allow access to existing files/directories outside of the /V8 directory. You will presumably still need to rewrite /<something> to /V8/<something>? Unless this is literally just a one page website with no external CSS, JS or images etc.?
    – MrWhite
    Nov 23, 2017 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.