redirect all non www to www. for Cakephp site - apache

Hi allm i'm having some issues in redirecting the non www pages to the www. pages for my cakephp site.
I've tried
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
but it doesn't seem to work.
My cakephp app resides in a subfolder. eg. www.domain.com/my.
i've added the above code to the root folder, www.domain.com/
any suggestions? thanks?
*update
This my .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^.*$ http://www.domain.com%{REQUEST_URI} [R=301,L]
i'm getting an extra webroot/index.php?url= inserted into the url. Instead of www.domain.com/my/apple, i'm getting www.domain.com/my/webroot/index.php?url=apple
thanks to all once again.

Your code is not quite right. See below.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^.*$ http://www.domain.com%{REQUEST_URI} [R=301,L] # <-- Mind the 'L'!
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]

if domain is .co.uk
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.co\.uk$ [NC]
RewriteRule ^.*$ http://www.domain.co.uk%{REQUEST_URI} [R=301,L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app/webroot/$1 [QSA,L]

I have tried the below code below and it worked for me:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^gigsadda\.com$ [NC]
RewriteRule ^.*$ http://www.gigsadda.com%{REQUEST_URI} [R=301,L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>

Related

Apache .htaccess, subfolder

I have website on Phalcon and everything works fine (I have SSL) but now I would like to have an access to the forum (https://www.example.com/forum/) but I have a problem with the .htaccess configuration.
Phalcon have the specific config:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^(.*)$ http://www.example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
What is the correct configuration for home domain and /forum/ folder?
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^ "http://www.example.com%{REQUEST_URI}" [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ "https://%{SERVER_NAME}%{REQUEST_URI}" [R,L]
RewriteCond %{REQUEST_URI} !^/forum/
RewriteRule ^.*$ public/$0 [L]
Phalcon's root .htaccess file redirects all traffic to /public folder.
What you need to do is add one more condition to your final rewrite rule. Here is portion of my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
# Force www
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
# Forward to public/
RewriteRule ^(forum)($|/) - [L]
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
This RewriteRule ^(forum)($|/) - [L] is the important condition you need to add before RewriteRule ^.*$ public/$0 [L]
Also you can add as much as rules you want to allow multiple folders.
RewriteRule ^(forum)($|/) - [L]
RewriteRule ^(some-other-folder)($|/) - [L]
...

Using directores for each domain using htaccess

I have a host and some domains. I want use a directory for each domain. Unfortunately my host dose't support vhosts. I made .htaccess file in root to redirect every domain request to it own directory.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} domain1\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /domain1/$1 [L]
RewriteCond %{HTTP_HOST} domain2\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /domain2t/$1 [L]
</IfModule>
It worked well and my site accessabe with "http://domain1.com" address. I try to use CodeIgniter as my sites framework.
So I configure base-url in CI config file to "http://domain1.com/domain1/" but it worked for main page only and when I request another controller like "http://doamin1.com/somecontroller", the page not found.
Also I setup .htaccess in site directory as follow.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /domain1
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteRule ^$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
How can I do this job so that browser addressbar do't show directory like
"http://doamain1.com/domain1/somecotroller" (this form is working).
Tanks everybody.
UPDATE
My routes.php.
$route['default_controller'] = 'main';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I after some tests got the answer that I need.
I change root .htaccess like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/domain2/
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.
RewriteRule ^(.*)$ /domain2/$1 [L]
RewriteCond %{REQUEST_URI} !^/domain1/
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.
RewriteRule ^(.*)$ /domain1/$1 [L]
</IfModule>
And every .htaccess in domain directory to as follow
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /domain1
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
Also change config.php file base-url to "http://domain1.com/"

htaccess Redirect Loop only on Home Page

All of a sudden this morning I'm having a problem with accessing the home page to my site.
I didn't make any changes so I'm not sure what the issue is. I'm guessing it has something to do with my .htaccess file.
All other pages to my site, I can access without a problem.
Below is my htaccess file. Any help would be appreciated. Thanks in advance.
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^hats-n-caps/(.*)$ http://www.domain-domain.com/apparel/hats-n-caps/$1 [R=301,L]
RewriteRule ^electronics/laser-pointers/(.*)$ http://www.domain-domain.com/desktop-office/laser-pointers/$1 [R=301,L]
#RewriteCond %{HTTP_HOST} !^www.domain-domain.com$ [NC]
#RewriteRule ^(.*)$ http://www.domain-domain.com/$1 [L,R=301]
RewriteCond %{REQUEST_URI} (^(.*)/site-map)
RewriteRule ^site-map$ /inc/pages/site-map.php [L]
RewriteCond %{REQUEST_URI} (^(.*)/site-map/products-([0-9]+))
RewriteRule ^site-map/products-([0-9]+)$ /inc/pages/site-map.php?type=product&page=$1 [L]
RewriteCond %{REQUEST_URI} (^(.*)/site-map/catalogs-([0-9]+))
RewriteRule ^site-map/catalogs-([0-9]+)$ /inc/pages/site-map.php?type=catalog&page=$1 [L]
RewriteCond %{REQUEST_URI} (^(.*)/site-map/pages-([0-9]+))
RewriteRule ^site-map/pages-([0-9]+)$ /inc/pages/site-map.php?type=pages&page=$1 [L]
RewriteCond %{REQUEST_URI} (^(.*)-p-(.*).html*)
RewriteRule ^(.*)-p-(.*).html$ product_info.php?products_id=$2&%{QUERY_STRING}[L]
RewriteCond %{REQUEST_URI} (^(.*)-c-(.*).html*)
RewriteRule ^(.*)-c-(.*).html$ product_info.php?catalog_id=$2&%{QUERY_STRING}[L]
RewriteCond %{REQUEST_URI} !(^(.*)-c-(.*).html*|^(.*)-p-(.*).html*|^/index.php|^/sitemap.xml.*|^/info.php.*|^/google_base_feed.xml.*|^/BingSiteAuth.xml.*|^/test.html.*|^/test.*|^/ajaxplorer.*|/livechat.*|^/phpmyadmin.*|^/ControlPanel.*|^/actikare.*|^/amwater.*|^/phi.*|^/sas.*|^/blog.*|^/cfscg.*|^/chase.*|^/cleaver.*|^/clubz.*|^/medicone.*|^/net2ftp.*|^/college-net.*|^/ducks.*|^/fast-teks.*|^/gatesgroup.*|^/guesthouse.*|^/neighborcare.*|^/pdf.*|^/reedrill.*|^/survey.*|^/temp.*|^/templates.*|^/tidewater.*|^/tuthill.*|^/uploads.*|^/upload.*|^/_include.*|^/product_info.*|^/server_request.*|^/server_request_products.*|^/admin.*|^/img/.*|^/images/.*|^/spaw2?|^/static/.*|^/tmp/|^/_include/|^/robots.txt|^/LiveSearchSiteAuth.xml|^/favicon.ico|/SiteReturn/.*|^/inc/pages/site-map.php(.*)|^/carousel_products.php)
RewriteRule (.*)$ /index.php [L]
RewriteCond %{HTTP_HOST} ^domain-domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain-domain.com$
RewriteRule ^/?$ "\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^domaindomain\.co$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domaindomain\.co$
RewriteRule ^/?$ "\/" [R=301,L]
This line:
RewriteRule ^/?$ "\/" [R=301,L]
Is what's causing your redirect loop. Get rid of the entire rule:
# RewriteCond %{HTTP_HOST} ^domaindomain\.co$ [OR]
# RewriteCond %{HTTP_HOST} ^www\.domaindomain\.co$
# RewriteRule ^/?$ "\/" [R=301,L]
It essentially redirects requests to the root / to itself.

Redirect HTTPS to HTTP and NON WWW to WWW

I have a problem with my redirects and im guessing its because i keep copy and pasting my htaccess rules.
I need to redirect all https traffic to http, and all non www to www. I also have some more rules to make pretty links
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule (.*\.php)\.$ /$1 [R=301,L]
RewriteRule ^people/([^/]*)/([^/]*)/([^/]*)$ /profile.php?county=$1&name=$2&id=$3 [L]
RewriteRule ^people-in-(.*) /people.php?county=$1 [L]
RewriteCond %{HTTP_HOST} ^ties\.co.uk$
RewriteRule (.*) http://www.ties.co.uk/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteRule (.*) / [R=301,L]
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
ErrorDocument 404 /notfound.php
You can combine:
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
and
RewriteCond %{HTTP_HOST} ^ties\.co.uk$
RewriteRule (.*) http://www.ties.co.uk/$1 [R=301,L]
into a single rule that needs to be right below RewriteEngine On:
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^ties\.co\.uk$ [NC]
RewriteRUle ^(.*)$ http://www.ties.co.uk/$1 [R=301,L]

.htaccess Apache URL Rewrite for Wordpress - permanently pointing one domain to another

I am trying to point www.olddomain.com/whatever to www.newdomain.com/whatever (as well as without the www.), but the Wordpress permalinks are not staying intact. Please help!!
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^olddomain.com [nc]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [r=301,nc]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [nc]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [r=301,nc]
</IfModule>
I fixed it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^olddomain.com [nc]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [r=301,nc,L]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [nc]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [r=301,nc,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
A much simpler solution. Change the .htaccess to just say:
RedirectMatch 301 /(.*) http://www.newdomain.com/$1
You will need to move this block:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
behind the redirects. It grabs everything and sends it to index.php.