I am redirecting an entire website, with a few exceptions. I have the following rules and conditions in place:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
#RewriteCond %{HTTP_HOST} !^loans\.myoldwebsite\.com$ [NC]
#RewriteCond %{HTTP_HOST} !^client\.myoldwebsite\.com$ [NC]
#RewriteRule ^(.*)$ https://www.mynewwebsite.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^(client|loans)\.myoldwebsite\.com$ [NC]
RewriteRule ^(.*)$ https://www.mynewwebsite.com/$1 [L,NE,R=302]
But I also need to make sure that http://www.myoldwebsite.com/getLead.php is ignored as well, so it doesn't get redirected.
You can have multiple RewriteConds:
RewriteCond %{HTTP_HOST} !^(client|loans)\.myoldwebsite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/getLead.php$
RewriteRule ^(.*)$ https://www.mynewwebsite.com/$1 [L,NE,R=302]
Related
I am trying to enforce https://www. on all of my pages except hotels.php.
My current code below is adding www. twice, i.e. https://www.www.example.com
.htaccess
RewriteEngine On
RewriteCond %{HTTPS} =off
RewriteCond %{REQUEST_URI} !^\/hotels.php\/
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} \/hotels.php\/
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [L,R=301]
Let me know what I am missing here which causing issue.
Thanks
Try these rewrite conditions:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force https:// for all except your desired URLs
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/hotels.php/ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force http:// for your desired URLs
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /hotels.php/ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You have to check if www is in the request URI as you check HTTPS. Try this:
//If has not www and it's not a subdomain adds www
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
//If has not https and it's not hotels.php add https
RewriteCond %{REQUEST_SCHEME} !https
RewriteCond %{REQUEST_URI} !^\/hotels.php\/
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Not a master with Htaccess regex, bit this works for me:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
</IfModule>
Hope it helps! There's lot of answers to this on StackOverflow... pretty sure that probably where I got the above!
I have an issue with mod_rewrite in apache2, I had enabled the mod_rewrite but doesent load I get this message: ERR_TOO_MANY_REDIRECTS
The .htaccess:
RewriteEngine on
RewriteRule ^(\w+)$ index.php?page=$1 [L,NC,QSA]
RewriteRule ^(\w+)+\/$ index.php?page=$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
IDK what is wrong.
Make the .htacess easier and clean.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^/(.*) http1://www.%{HTTP_HOST}%/$1 [R=301]
RewriteCond "%{REQUEST_URI}" "!=/index.php" RewriteRule "^(.*)" "/index.php?page=$1" [L,PT]
It solution works without SSL, as main step.
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.*).domain\.com
RewriteRule ^(.*)$ load.php?id=%1&q=$1 [NC,QSA,L]
This is my current code
subdomain.domain.com/querystring
The condition is domain.com
Is there a way I can achieve the same thing
but the domain.com can be dynamic, means it could be 123456.co or qwerty1234.xyz/querystring
Yet it able capture
Domain: qwerty1234.xyz
Query String: querystring
How do I make my htaccess to check if domain is not containing domain.com, it will use load.php
but pass the domain & its query string to load.php as ?domain=$domain&query=$querystring
Thanks!
Updated .htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.*).domain\.com
RewriteRule ^(.*)$ load.php?id=%1&q=$1 [NC,QSA,L]
RewriteCond %{REQUEST_URI} !(\.)?(css|js|png|jpg|gif|robots\.txt)$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)$
RewriteRule ^(.*)$ load.php?id=%1&q=$1 [NC,QSA,L]
You should be able to add this to your rules. You don't have to do anything special to capture the querystring. Simply keep the QSA flag and any additional query string on the request will also be sent to load.php.
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.)?(css|js|png|jpg|gif|robots\.txt)$ [NC]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com
RewriteCond %{HTTP_HOST} ^(.*)$
RewriteRule ^(.*)$ load.php?domain=%1 [NC,QSA,L]
I have a wildcard subdomain *.domain.com assigned to public_html/.
I want to do like this:
For example, /folder1/index.php is based on state name(?state=statename).
For the /folder1/folder2/index.php, it will be based on unique name(?name=uniquename).
So, www.domain.com/folder1/index.php?state=statename will be statename.domain.com
and www.domain.com/folder1/folder2/index.php?name=uniquename will be uniquename.domain.com
This is my code
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder1/index\.php\?state=([^\s&]+) [NC]
RewriteRule ^ http://%1.domain.com/? [R=301,L]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder1/folder2/index\.php\?name=([^\s&]+) [NC]
RewriteRule ^ http://%1.domain.com/? [R=301,L]
The problem is it redirect back to public_html directory. Is there any problem with the code?
Old Code
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^$ /index [L]
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteRule ^(index)/?$ /$1.php?name=%2 [L,NC,QSA]
Code explanation : Whenever user enter uniquename.domain.com, it will automatically go to www.domain.com/index.php?name=uniquename and the uniquename.domain.com in url bar would not change.
The differences for the new problem is there are different state directory and the domain would be state1.uniquename.domain.com. The 'state1.uniquename.domain.com' in the url bar should not change too.
Based on your comments. Make sure DOCUMENT_ROOT for www.domain.com, state1.domain.com, state2.domain.com is public_html
Try this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder1/index\.php\?state=([^\s&]+) [NC]
RewriteRule ^ http://%1.domain.com/? [R=301,L]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder1/folder2/index\.php\?name=([^\s&]+) [NC]
RewriteRule ^ http://%1.domain.com/? [R=301,L]
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.domain\.com$ [NC]
RewriteRule ^ /state/%1/client/index.php?name=%2&page=%{REQUEST_URI} [L,NC]
Simplified Question
I changed domain.com/folder/client.php?id=1 to 1.domain.com with this .htaccess code
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteRule ^$ /client.php?id=%2 [QSA,NC]
Now I want to change domain.com/folder/about.php?id=1 to 1.domain.com/about.
Is it possible ? or is there any other ways to do this?
Thank you :D
Old Question
In my directory, I have 2 pages (client.php, about.php). I've already set a wildcard subdomain to the page directory and changed domain.com/folder/client.php?id=1 to 1.domain.com. This is my .htaccess code :
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteRule ^$ /client.php?id=%2 [QSA,NC]
Now, I need to add another page. So, in client.php page, there's a button to go to about.php page in the same directory. I tried 1.domain.com/about.php and it's not working.
Do I have to create another line of RewriteCond ? or is there any other ways to do this?
Thank you very much :D
Updated Code without working id
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/about [NC]
RewriteRule ^$ /client.php?id=%2 [QSA,NC]
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/about [NC]
RewriteCond %{REQUEST_URI} !about\.php [NC]
RewriteRule . /about.php?id=%1 [L,QSA]
**The code resolved the text in url box. Let's say id=1. Now it's showing 1.domain.com/about.
the php code : `$_GET["id"]` is not specified.**
Thanks to faa for the edit :D
The Full Solution
Refer Using mod-Rewrite with 5 pages (wildcard sub-domain)
You may try this:
Options +FollowSymLinks
RewriteEngine on
## This is the actual rule-set that works as it is according to the OP
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
# Added to exclude folder `/about`
RewriteCond %{REQUEST_URI} !^/about [NC]
RewriteRule ^$ /client.php?id=%2 [L,QSA,NC]
## This is the additional rule set
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/about [NC]
RewriteCond %{REQUEST_URI} !about\.php [NC]
RewriteRule . http://domain.com/folder/about.php?id=%1 [L,QSA]
Maps silently
http://N.domain.com/about with or without trailing slash
To:
http://domain.com/folder/about.php?id=N
For permanent and visible redirection, replace [L,QSA] with [L,R=301,QSA]