3

I need a solution for the following problem:

I use a CMS and want the backend to only be available at http://example.com/backend and not at http://www.example.com/backend. The rest of the site should be available at www.example.com.

How do I change my .htaccess file to achieve this? I already have a rewrite rule from (non-www) to www.

Here's what I currently have in my .htaccess file:


  ##
  # Uncomment the following lines to add "www." to the domain:
  #
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
  #
  # Uncomment the following lines to remove "www." from the domain:
  #
  #   RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
  #   RewriteRule (.*) http://example.com/$1 [R=301,L]
  #
  # Make sure to replace "example.com" with your domain name.
  ##

So, the first bit is the redirect from non-www to www. It works on the domain part of the URL. As explained, I need a rewrite rule from the backend login at http://www.example.com/backend to http://example.com/backend.

2

It sounds like you have a specific directory that you want to be accessed without www while everything else would be accessed with it.

You will need both rewrite rules (one of which you currently have commented out) and you will need to add some extra conditions to each:

RewriteCond %{REQUEST_URI} !^/backend
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(backend.*) http://example.com/$1 [R=301,L]
| improve this answer | |
  • 1
    ^/backend$ - The $ (end of string anchor) should be removed from the end of the CondPattern in order to match all URLs that just start "/backend". In the case of the 2nd (backend) redirect, you can include the URL-path in the RewriteRule pattern instead of having a separate condition - marginally more efficient. ie. RewriteRule ^(backend.*) http://example.com/$1 [R=301,L]. – MrWhite Jan 4 '16 at 7:33

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.