Mobile app version of vmapp.org
Login or Join
Welton855

: .htaccess RewriteRule leading to unexpected page 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

@Welton855

Posted in: #Htaccess #ModRewrite #Redirects #UrlRewriting

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/ [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:seb@example.com'>seb@example.com</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/ [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?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Welton855

2 Comments

Sorted by latest first Latest Oldest Best

 

@Frith620

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: sebmuller.com

10% popularity Vote Up Vote Down


 

@Alves908

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/ [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/ [L]


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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme