0

I have a site that needs to exist in a subfolder

example.com/site

But i'm trying to use the .htaccess to remove any links that contain www (to make sure codeigniter csrf doesn't throw errors), so i've added

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

This works well when there is a page identifier specified, so

www.example.com/site/book rewrites to example.com/site/book

But when there is no page identifier specified I get a 404

www.example.com/site rewrites to example.com/site//usr/local/pem/vhosts/103480/webspace/httpdocs/new

I was wondering if anybody could point me in the right direction?

This is my full .htaccess

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule ^(.*)$ http://example.com/site/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 
JasonM
  • 165
  • 12
  • 1
    Do you have an additional htaccess file in the `site` subdirectory? – Olaf Dietsche Apr 23 '13 at 14:55
  • Yes, the .htaccess in question is in the `site` subdirectory, sorry i should have been clearer. The updated answer from faa has solved my problem though – JasonM Apr 23 '13 at 15:18

2 Answers2

1

You may try this instead:

RewriteCond %{HTTP_HOST} ^www\.    [NC]
RewriteCond %{REQUEST_URI} ^/(.*)  [NC]
RewriteRule .*  http://example.com/%1 [R=301,L]
Felipe Alameda A
  • 11,791
  • 3
  • 29
  • 37
  • Thanks for your answer. What you've suggested doesn't include the redirection to the subfolder, and when i add it back in `RewriteRule ^(.*) http://example.com/site/$1 [R=301,L]` it rewrites to the same url as mentioned in the question when trying to access `www.example.com/site` – JasonM Apr 23 '13 at 13:56
  • The code in my answer removes `www` from the request. That's all. Now, do you want to redirect everything to directory `/site`? If so, you have to make sure first the request is not to `/site`. I can update my answer if you explain more in detail what you want. – Felipe Alameda A Apr 23 '13 at 14:06
  • I want to redirect the request as: www.example.com/site => example.com/site and www.example.com/site/book => example.com/site/book What i have works for the second case but not for the first – JasonM Apr 23 '13 at 14:20
  • Well, that's exactly what the code in my answer does. Just removes the `www` and keeps the URI-path as requested. You don't have to add anything. Replace the top 2 lines in your question with those in my answer. – Felipe Alameda A Apr 23 '13 at 14:24
  • When i add in your two lines `www.example.com/site/book` redirects to `example.com/book`, it removes the subfolder – JasonM Apr 23 '13 at 14:42
  • There is something missing. The behavior you describe makes no sense, the whole URI-path is being passed to the substitution URL in the rule in my answer. If you are not adding your original rule and there is no more code, something doesn't fit. Is `site` by any chance a sub-domain? – Felipe Alameda A Apr 23 '13 at 14:53
  • Yeah, the whole codeigniter structure is installed on `site`, `example.com` is a different site. And the .htaccess is within `site`. Thank you for your help, i appreciate your time, maybe i'll try come at the problem from a different angle – JasonM Apr 23 '13 at 15:01
  • Modified my answer. Give it a try. – Felipe Alameda A Apr 23 '13 at 15:02
1

Maybe, you're just missing a RewriteBase

Depending on where the .htaccess file is, try either

RewriteBase /

or

RewriteBase /site

Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198