2

I am subfolder redirecting from www.example.com to www.example.com/folder

Here is my .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} example.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://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 www.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 www.example.com.

  • 1
    Are you hosting multiple domains on the same account? Do you have other directives in your .htaccess file? – MrWhite Dec 12 '16 at 14:23
  • Server configuration questions are off topic here and belong on serverfault.com: webmasters.stackexchange.com/help/on-topic Server Fault participants are much more likely to know the technical details on topics such as DNS and Apache configuration than people who answer questions here. – Rob Dec 12 '16 at 14:39
  • 2
    @Rob This question is perfectly on-topic for webmasters - this is userland config that directly relates to running a website (aka URL rewriting). Unless you are managing your own server (a sysadmin) then strictly speaking this would be off-topic on ServerFault. – MrWhite Dec 12 '16 at 15:08
  • @w3dk So you are saying .htaccess files are not Apache configuration? – Rob Dec 12 '16 at 15:33
  • @w3dk Actually, given that it's URL rewriting and, specifically, userland, I see your point but I'll have to think about whether I agree it's on topic or not. – Rob Dec 12 '16 at 15:38
2

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 www.example.com/ to www.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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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