.htaccess reescribir en subcarpeta con codeigniter rewrite

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] 

preguntado el 23 de abril de 13 a las 13:04

Do you have an additional htaccess file in the site ¿subdirectorio? -

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 -

2 Respuestas

Puede intentar esto en su lugar:

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

Respondido 23 Abr '13, 16:04

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

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.

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

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.

That's it! Thanks again for your time (and patience!) - jasonm

Maybe, you're just missing a RewriteBase

Dependiendo de donde .htaccess file is, try either

RewriteBase /

or

RewriteBase /site

Nunca prueba con 301 habilitado, vea esta respuesta Consejos para depurar las reglas de reescritura de .htaccess para más detalles.

contestado el 23 de mayo de 17 a las 12:05

Thanks for the link olaf, that will be good reading for me to try and improve in this area - jasonm

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.