A simple mod re-write rule doesnt work with htaccess - .htaccess

I want the user to be redirected whenever he reaches my subdomain
Here is what is inside my htaccess:
RewriteEngine On
RewriteRule ^http://smale.deals.com/(.*) http://traual.deals.com/$1 [R=301,L]
RewriteRule ^http://deals.com/smale/(.*) http://deals.com/traual/$1 [R=301,L]
But no redirect happens. why?
I also have got this in my root htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} (Android|iPhone|iPod|Blackberry) [NC]
RewriteCond %{REQUEST_URI} !^/mobile [NC]
RewriteRule ^(.*)/?$ mobile/$1 [L]

You cannot include the protocol and domain in RewriteRule. Those need to be accounted for in RewriteCond:
RewriteEngine On
# Rewrite requests to smale.deals.com to traual.deals.com
RewriteCond %{HTTP_HOST} ^smale\.deals\.com$ [NC]
RewriteRule (.*) http://traual.deals.com/$1 [R=301,L]
# For deals.com...
RewriteCond %{HTTP_HOST} ^deals\.com$ [NC]
# Rewrite requests to smale/ to deals.com/traual/
RewriteRule ^smale/(.*) http://deals.com/traual/$1 [R=301,L]

Related

htaccess language rediretion

I have a website, www.thesite.com and an alias www.lesite.com
i need a htaccess redirection:
I would like that, when I go to www.thesite.com/fr, it redirects me to www.lesite.com/fr
Likewise, when I go to www.lesite.com/en, I would like it to redirect me to www.thesite.com/en
I have tried different methods but i only succeed to create infinite loops !----
Options +FollowSymlinks
RewriteRule ^fr$ http://dev.mariage.ch/fr/ [L]
RewriteRule ^de$ http://dev.hortzeit.ch/de/ [L]
OR
RewriteCond %{HTTP_HOST} !^dev\.hortzeit\.ch\/de\/
RewriteRule (.*) http://dev.hortzeit.ch/de$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^dev\.mariage\.ch\/fr\/
RewriteRule (.*) http://dev.mariage.ch/fr$1 [R=301,L]
You can't use path as part of RewriteCond for HTTP_HOST
instead of dev.hortzeit.ch/de you must use just host part dev.hortzeit.ch
RewriteEngine On
RewriteCond %{HTTP_HOST} !^dev\.hortzeit\.ch [NC]
RewriteRule ^de(/?.*)$ http://dev.hortzeit.ch/de$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} !^dev\.mariage\.ch [NC]
RewriteRule ^fr(/?.*)$ http://dev.mariage.ch/fr$1 [R=301,NC,L]

htaccess rewrite rule for subdomains with page id

i want to redirect
http://subdomain.example.com/index.php?pageID=45
to
http://example.com/subdomain/45.html
Please help me with this
i tried
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.example.com/sudomain/$1 [L,R=301]
You can replace your code by this one in your htaccess (in root folder)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^pageID=([1-9][0-9]*)$ [NC]
RewriteRule ^index\.php$ http://example.com/subdomain/%1.html? [R=301,L]

Disable .htaccess rule to a specific directory

I have a Rule in My base folder .htaccess file. The rule will automatically redirect http:// to https://. But for a specific folder or URL I need to access through http://
RewriteRule ^(.*)$ https:/%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
For example My website will be like https://<>/power/a/1
But I need to access some URL without https://
expected http://localhost/admin/rest_api/api_methods/
You need to implement an "exception rule" in form of a condition for the RewriteRule:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/webservices/access
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteEngine on
RewriteBase /
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
# Turn SSL on for /user/login
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/admin/rest_api
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Turn SSL off everything but /user/login
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/admin/rest_api
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
This should work:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/webservices/access
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
It will redirect everything but the /webservices/access folder (or any other links in that folder like /webservices/access/login.php) to https

Redirect www to non-www with htaccess

I try to redirect my website from www to no-www for whole content but I don't know how I get it work, because I've got two rewrite rules already.
This is my htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^meaning-first-name\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^([^/\.]+)/?$ index.php?name=$1 [L]
RewriteRule ^([^/\.]+)?$ index.php?name=$1 [L]
RewriteRule ^voorletter/([^/\.]+)/?$ initial.php?letter=$1 [L]
RewriteRule ^voorletter/([^/\.]+)?$ initial.php?letter=$1 [L]
Now is working:
website.com/John* shows: *index.php?name=John
website.com/initial/J* shows: *initial.php?letter=J
But how can I include redirect for www to non-www to this?
Remove the 2 conditions on top and add this instead:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

Domain Management using .htaccess

Suppose all the requests on A.com and B.com end up on the same server, and I want to control the request using htaccess.
The default www content root is /public_html/, but I want A.com requests to be forwarded to /public_html/A/ and B.com requests should be forwarded to /public_html/B/
I came up with this solution:
#/public_html/.htaccess
# A.com
RewriteCond %{HTTP_HOST} ^A.com$
RewriteCond %{REQUEST_URI} !^/A/
RewriteRule (.*) A/$1 [L]
# B.com
RewriteCond %{HTTP_HOST} ^B.com$
RewriteCond %{REQUEST_URI} !^/B/
RewriteRule (.*) B/$1 [L]
and I am having two problems with it:
A.com/A/index.php and A.com/index.php are the same thing, which is not cool! I'd rather the user be 301-ly redirected to the latter whenever he uses the former.
A.com/etc redirects to A.com/A/etc/
Overall, I don't want my visitors to see the /A/ (or /B/) in the URL anyway. Any htaccess solution is welcome.
You can use this code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
# for external redirection of A.com/A/foo to A.com/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/*(A|B)(?:/(.*)|)\s [NC]
RewriteRule ^ http://%1.com/%2 [R=301,L,NC]
# for internal redirection of A.com/foo to A.com/A/foo
RewriteCond %{HTTP_HOST} ^(A|B)\.com$ [NC]
RewriteRule (?!^(A|B)(?:/(.*)|)$)^.*$ %1%{REQUEST_URI} [L]
Add this to your .htaccess in the same order:
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteRule ^(A|B)(?:/(.*))?$ http://%{HTTP_HOST}/$2 [NC,L,R=301]
#/public_html/.htaccess
# A.com or B.com
RewriteCond %{HTTP_HOST} ^(A|B).com$ [NC]
RewriteRule (.*) %1/$1 [L]
If you do not want A to be redirected to A when accessed like this A.com/B/. or If you do not want B to be redirected to B when accessed like this B.com/A/. You can throw a 404 error.
RewriteCond %{HTTP_HOST} ^A.com$ [NC]
RewriteCond %{REQUEST_URI} ^/B(?:/(.*))? [NC]
RewriteRule ^ - [L,R=404]
RewriteCond %{HTTP_HOST} ^B.com$ [NC]
RewriteCond %{REQUEST_URI} ^/A(?:/(.*))? [NC]
RewriteRule ^ - [L,R=404]
Add the above lines right afterRewriteRule ^ - [L] before RewriteRule ^(A|B)(?:/(.*....

Resources