Mobile app version of vmapp.org
Login or Join
Lengel546

: 301 subfolder redirect but want old URL to remain in address bar I am subfolder redirecting from www.example.com to www.example.com/folder Here is my .htaccess: <IfModule mod_rewrite.c> RewriteEngine

@Lengel546

Posted in: #301Redirect #Htaccess #Redirects

I am subfolder redirecting from example.com to example.com/folder
Here is my .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} example.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ www.example.com/website/index.php [R=301,NC]
</IfModule>


Except for one thing, this is 95% working.

The root URL redirects to the /website subfolder.
Both example.com and example.com resolve and are redirected.
And www is prefixed to example.com/website.

However, the final thing I want to do is make the URL remain in the browser window, so that if a user clicks a bookmark, or types in (www)example.com, then the visual served content is from the redirected index.php, but in the browser address bar they still see example.com.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Lengel546

1 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

In order to keep the same URL in the address bar, you need an internal rewrite, as opposed to an external redirect (that's the R=301 bit).

Try the following instead:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteRule ^$ website/index.php [L]


Notice that the R flag is removed from the RewriteRule and the substitution is now relative, not absolute. (An absolute URL-path would implicitly trigger a redirect.)

In the code you posted, the RewriteBase directive is not required. Neither is the second RewriteCond directive that checks against REQUEST_URI - I've moved the URL check to the RewriteRule pattern instead, which is more efficient. The NC (nocase) flag is also not required on the RewriteRule in this instance.

Unless you have multiple domains on your account then the first RewriteCond directive that checks against HTTP_HOST is also not required.

And unless this is intended to work without mod_rewrite installed then you should remove the <IfModule> wrapper as well.

Make sure you clear your browser cache before testing, as the previous 301 redirect will have been cached.

Note that this is a single rewrite from example.com/ to example.com/website/index.php. Any other URL is not rewritten/redirected. So, any internal links will need to contain the actual root-relative (or absolute) URLs.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme