htaccess redirection - stop redirecting subdomain - apache

I have this code in my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
RedirectMatch 301 ^/de/(.*)$ http://de.example.com/$1
My problem is that it redirects even http://xyz.example.com/de/ to http://de.example.com/
What I need is still redirecting from example.com/de to de.example.com
but without redirecting xyz.example.com/de to de.example.com
Could you help me to modify that?

You ar mixing directives from 2 different Apache modules mod_rewrite and mod_alias. Stick to mod_rewrite only and have this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^de/(.*)$ http://de.example.com/$1 [L,NC,R=302]

Related

redirect from not-www to www prestashop .htaccess

I have a problem with my prestashop website. For some reasons, if I put in the domain name "www.mysite.com" some modules stop working. So, I have to use "mysite.com". Problem is that I want to be displayed www.mysite.com on the browser. I know that there is set of rules to be added to .htaccess to redirect not-www to www:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
but this doesn't work with the rewritebase on prestashop's .htaccess
RewriteEngine on
RewriteRule . - [E=REWRITEBASE:/]
RewriteRule ^api/?(.*)$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]
how can I merge the two rules?
Thank you very much
Try this default .htaccess file
Default htaccess file
and also check if mod rewrite is enable. If this is not working you can do a 301 redirection to www version.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

Redirecting Subdirectory to Subdomain with either htaccess or cpanel

I want to redirect a subdirectory to a subdomain either using .htaccess or cpanel
redirects from
domain.in/subfolder
to
subfolder.domain.in
i m using cakephp app for both domain.in and subfolder.domain.in
each have separate core library
Please help me to solve this
Thanks in advance
You can use this rule as very first rule in your /subfolder/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.in$ [NC]
RewriteRule (.*) http://subfolder.domain.in/$1 [NE,R=302,L]
Or else:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(domain\.in)$ [NC]
RewriteRule (.*) http://subfolder.%1/$1 [NE,R=302,L]
Using the following .htaccess file might work:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.in [NC]
RewriteRule ^(.*)$ http://www.domain.in$1 [L,R=301]
RedirectMatch 301 ^/subfolder/(.*)$ http://subfolder.domain.in/$1

Stop Apache mod_rewrite affecting Redirect rules

I have the following .htaccess file:
Redirect 301 /test/example-1 /test/example-2
RewriteEngine On
RewriteBase /test/
RewriteRule ^(.*)$ index.php?_url=$1 [QSA,L]
I want the redirect to happen without mod_rewrite interfering with it, but at the moment when you hit /test/example-1 it redirects to /test/example-2?_url=example-1.
Is there any way to stop the query parameter from the rewrite rule getting append to the redirect URL? Thanks.
Have your rewrite rule as:
RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_URI} !/(example-1|index\.php) [NC]
RewriteRule ^(.*)$ index.php?_url=$1 [QSA,L]
But in general you shouldn't mix mod_alias rules with mod_rewrite rules. Your code can be rewritten as:
RewriteEngine On
RewriteBase /test/
RewriteCond %{THE_REQUEST} /example-1[\s?/] [NC]
RewriteRule ^ example-2 [R=301,L]
RewriteRule ^(.*)$ index.php?_url=$1 [QSA,L]

Bypass/exclude a specific set of URLs from a rewriteRule

I'm trying to put a mobile site live on a subdomain. e.g. m.domain.com there is already a desktop site on domain.com.
The domain.com htaccess file has a rewrite rule to redirect all non-www requests to www.domain.com. This is conflicting with the m. subdomain, causing the user to be taken to www.m.domain.com.
Can I add some kind of exclusion to the rewrite rule? Or perhaps specifically overrule the rewriterule?
My non-www to www rewriteRule is as follows:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
You can use this rule to avoid impacting m.domain.com from www rule:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

How can I use mod_rewrite to 301 redirect example.com to www.example.com?

I need to redirect any URLs without "www." to URLs with "www." for better search engine optimization. I read that this is possible with mod_rewrite and .htaccess files, but I do not know the right code to use. Can anyone help?
Create a file called .htaccess in your root folder (the one where, say, index.html or index.php resides). Put the following into it:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
There is an excellent example of this in Apache's URL Rewriting Guide.
The following code would redirect any non-www request to a www request:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R=301]
You'd want to put this inside the <Directory> directive of your .htaccess file, or apache config file.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) http://www.example.com/$1 [L,R=301]
To remove www from your url website use this code on .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
To force www in your website url use this code on .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^YourSite.com$
RewriteRule ^(.*)$ http://www.yourSite.com/$1 [R=301]
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.html [R=301,L]
Were "YourSite.com" you must replace for your url.
In addition to using mob_rewrite you can do this with a virtual host directive
<VirtualHost example.com>
ServerName example.com
Redirect permanent / http://www.example.com
</VirtualHost>
I usually do it the other way around to remove the extraneous 'www'.