my project has multiple domains and I would like to redirect 100 pages for 1 of these domain.
Now I found this post on how to set up conditions for 1 domain.
RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC]
RewriteRule ^(.*)$ index.php?lang=it [NC,QSA]
RewriteCond %{HTTP_HOST} ^www\.site2\.com [NC]
RewriteRule ^(.*)$ index.php?lang=en [NC,QSA]
My question would be, how can I set this up for 100 pages?
Do I have to duplicate the condition for each link?
What I want to do is this
RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC]
RewriteRule ^some https://www.mypage.de/shop/some [R=301, L]
RewriteRule ^page https://www.mypage.de/shop/page [R=301, L]
RewriteRule ^settings https://www.mypage.de/shop/settings [R=301, L]
There are so many scenarios to do in this case , for example , you could redirect every page except specific page like this :
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC]
RewriteCond %{REQUEST_URI} !^/(page1|page2|page3|whatever)
RewriteRule ^(.*)$ https://www.mypage.de/shop/$1 [R=301, L]
But , if the number of page who should be excluded is large , you could create directory , for example , new then move all files that have to be redirected at it , then make this rule
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC]
RewriteCond %{REQUEST_URI} !^/new/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/new%{REQUEST_URI} -f
RewriteRule ^(.*)$ /new/$1 [L]
Then you could add this to redirect any URI contains new to that new location :
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC]
RewriteCond %{REQUEST_URI} ^/new/
RewriteRule ^(.*)$ https://www.mypage.de/shop/$1 [R=301, L]
So code should look like this :
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC]
RewriteCond %{REQUEST_URI} !^/new/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/new%{REQUEST_URI} -f
RewriteRule ^(.*)$ /new/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC]
RewriteCond %{REQUEST_URI} ^/new/
RewriteRule ^(.*)$ https://www.mypage.de/shop/$1 [R=301, L]
Assuming your page names are same after redirect as you've shown in your question, you can use a single rule like this as your top most rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.site1\.com$ [NC]
RewriteRule .+ https://www.mypage.de/shop/$0 [R=301,NE,L]
Make sure to use a new browser for your testing.
Probably Skip flag will be the best aproach.
It looks like this:
RewriteCond %{HTTP_HOST} !^www\.site1\.com [NC]
RewriteRule .? - [S=100]
RewriteRule ^some https://www.mypage.de/shop/some [R=301, L]
RewriteRule ^page https://www.mypage.de/shop/page [R=301, L]
RewriteRule ^settings https://www.mypage.de/shop/settings [R=301, L]
.....
RewriteRule ^rule_for_100nd_page https://www.mypage.de/shop/settings [R=301, L]
How this works.
Test HOST if NOT skip 100 (S=100) the next RewriteRules if YES RewriteRule .? - [S=100] is ignored
Write your 100 rules for www.site1.com source domain and go on.
RewriteRule ^(.*)$ https://www.mypage.de/shop/$1 [QSA]
This will redirect every single page.
If you only want 100 specific pages you must add one by one. Like other answers were provided.
So after testing some of the suggestions I ended up having to add the condition to each rule in order to make it work.
So as far as I can tell you can't set conditions for more than one rule if the targets are to specific.
So my solution for this issue looks like this.
Thanks for all the suggestions.
RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC]
RewriteRule ^some https://www.site1.de/shop/some [R=301, L]
RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC]
RewriteRule ^page https://www.site1.de/random/page [R=301, L]
RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC]
RewriteRule ^settings https://www.site1.de/whatever/settings [R=301, L]
Related
I have a 2 php pages "download.php" and "windows.php"
the link of the both is
xyn.com/download.php?r=test
xyn.com/windows.php?r=test
Note : the two variable in the two pages are the same
I want to make the link of the pages like this
1 - download."variable".domain.com -> download.php?r=test
2 - download."variable".domain.com/windows -> windows.php?r=test
the code I am using is :
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} !index\.php
RewriteCond %{HTTP_HOST} ^download\.(.+?)\.domain\.com$
RewriteRule ^(.*)$ download.php?r=%1 [L]
RewriteCond %{HTTP_HOST} ^download\.(.+?)\.domain\.com/windows$
RewriteRule ^(.*)$ windows.php?r=%1 [L]
the first link is working fine :
download.variable.domain.com
but the second link is taking me to the first link :
download.variable.domain.com/windows
so any idea ?
RewriteCond %{THE_REQUEST} \h/\h
RewriteCond %{REQUEST_URI} !^/download\.php
RewriteCond %{HTTP_HOST} ^download\.([^\.]+)\.domain\.com$
RewriteRule ^ download.php?r=%1 [L]
RewriteCond %{THE_REQUEST} \h/windows\h
RewriteCond %{REQUEST_URI} !^/windows\.php
RewriteCond %{HTTP_HOST} ^download\.([^\.]+)\.domain\.com$
RewriteRule ^ windows.php?r=%1 [L]
all these subdomains should point to one document root
Both conditions are on same HTTP_HOST, just add the REQUEST_URI as a condition.
Try this:
RewriteCond %{REQUEST_URI} !^/windows
RewriteCond %{HTTP_HOST} ^download\.(.+?)\.YourDomain\.net$
RewriteRule ^ http://YourDomain.net/download.php?r=%1 [L]
RewriteCond %{REQUEST_URI} ^/windows
RewriteCond %{HTTP_HOST} ^download\.(.+?)\.YourDomain\.net$
RewriteRule ^ http://YourDomain.net/windows.php?r=%1 [L]
http://download.babjhons.YourDomain.net/windows
=>
http://YourDomain.net/windows.php?r=babjhons
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 am trying to just update my .htaccess file so that I get nicer looking url's (without the file extenstion of .php). The file already has a redirect in it as I have both domain names.
Here is the original code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^darrenmorton\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.darrenmorton\.com$
RewriteRule ^/?$ "http\:\/\/darrenmorton\.co\.uk\/" [R=301,L]
And here is my updated code that is not working
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^darrenmorton\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.darrenmorton\.com$
RewriteRule ^/?$ "http\:\/\/darrenmorton\.co\.uk\/" [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
Not
RewriteCond %{REQUEST_FILENAME} \.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^contact-darren-morton.php$ contact-darren-morton.php [L]
Not sure what is the problem as I am new to .htaccess files!
You can use:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?darrenmorton\.com$ [NC]
RewriteRule ^ http://darrenmorton.co.uk}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
You are redirecting everything with that statement:
RewriteRule ^/?$ "http\:\/\/darrenmorton\.co\.uk\/" [R=301,L]
Nothing after that will ever work, because it is the last rule to be checked if it matches (and it matches always). Defined by you with the [L].
So at least you need to change the order of your rules. If you want the rules to only work on your .co.uk domain you have other options, too.
I am using codeigniter framework i need to force to remove www from the url so I am using this code
RewriteEngine on
RewriteCond $1 !^(index\.php|css|images|js|plugins|scripts|fancybox|uploads|mobile|robots\.txt)
RewriteRule ^(.*)$ /framework/index.php?/$1 [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI}/$1 [R=301,QSA,NC,L]
This code is forcing removal of www. but the problem is when a user access a link with www
eg:www.mydomain.com/framework/article/sometestarticle368/
It is redirecting to
www.mydomain.com/framework/
How can i fix this ?
Change the order of your rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI}/$1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|css|images|js|plugins|scripts|fancybox|uploads|mobile|robots\.txt)
RewriteRule ^(.*)$ /framework/index.php?/$1 [L,QSA]
Otherwise your 2nd rules runs first and change the URI to /framework/... before the www removal rule..
Try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]
Thanks
I want to force www in my urls. I'm having problems with writing. I know that this code will do the trick.
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1
But I want to put all in $_GET['page']
RewriteRule ^(.*)$ /index.php?page=$1 [L]
How should I put this together?
You need to add [QSA], which allows you to modify the query string of the URL:
RewriteRule ^(.*)$ /index.php?page=$1 [QSA,L]
this should do the job
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule .* index.php?page=$0 [L]