Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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]
share|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]. – w3dk Jan 4 at 7:33
1  
@w3d thanks! I've edited those corrections in – Stephen Ostermiller Jan 4 at 11:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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