I'm trying to point a domain name to a single page, and keep the domain the same (no redirect).
So if a user types: www.domain1.com.au --> original site is shown
If a user types: www.domain2.com.au --> they are shown www.domain1.com.au/second.php, but the URL still says www.domain2.com.au.
Can this be done using .htaccess?
Snippet of current .htaccess file is:
RewriteCond %{HTTP_HOST} www\.domain2\.com\.au [NC]
RewriteRule (.*) /two [L]
RewriteCond $1 ^(one|two|three|four) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
So basically the www.domain2.com.au needs to display the www.domain1.com.au/two page, which really is www.domain1.com.au/index.php/two
You could definitely do something like this:
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com\.au$
RewriteCond %{REQUEST_URI} !^/domain1
RewriteRule (.*) /domain1/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com\.au$
RewriteCond %{REQUEST_URI} !^/domain2
RewriteRule (.*) /domain2/$1 [L]
Your exact case would similar to this:
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com\.au$
RewriteRule (.*) /second.php [L]
Edit: Now that you've posted a snippet try this instead:
RewriteCond %{HTTP_HOST} www\.domain2\.com\.au [NC]
RewriteRule (.*) /index.php/two [L]
RewriteCond $1 ^(one|two|three|four) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
You want to show a different site than what the URL says.
That requires proxying.
Read this: http://httpd.apache.org/docs/2.0/mod/mod_proxy.html
Related
I'm trying to use .htaccess to redirect a visitor with a specified IP address to always be redirected to a specified page when visiting any page on my website. I've tried the code below but it is causing an error. I'm using a wordpress site.
RewriteCond %{REQUEST_URI} !/specified-page/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC]
RewriteCond %{REMOTE_HOST} !^1\.2\.3\.4
RewriteRule $ /specified-page/ [R=302,L]
It is causing "ERR_TOO_MANY_REDIRECTS" message.
As a side-note, my domain is pointing to a sub-folder in my webspace. As are some other websites. This is all directed from another .htaccess file in the root of my webspace with code like this.
RewriteCond %{HTTP_HOST} ^([^.]+\.)?mydomain1\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mywebsitefolder1/
RewriteRule (.*) /mywebsitefolder1/$1 [L]
RewriteCond %{HTTP_HOST} ^([^.]+\.)?mydomain2\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mywebsitefolder2/
RewriteRule (.*) /mywebsitefolder2/$1 [L]
and so on...
(The .htaccess I'm trying to edit to create the IP redirect is in 'mywebsitefolder1' folder.)
Try it like that:
RewriteCond %{REQUEST_URI} !/specified-page/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC]
RewriteCond %{REMOTE_HOST} !^1\.2\.3\.4
RewriteRule ^ /specified-page/? [R=302,L]
This will also remove an optional query string that could cause the redirect loop.
You have indeed a looping condition. Let's try it like this:
RewriteCond %{REMOTE_HOST} ^1\.2\.3\.4
RewriteCond %{REQUEST_URI} !/specified-page/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC]
RewriteRule ^ /mywebsitefolder1/specified-page/ [R=302,L]
RewriteCond %{HTTP_HOST} ^([^.]+\.)?mydomain1\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mywebsitefolder1/
RewriteRule (.*) /mywebsitefolder1/$1 [L]
RewriteCond %{HTTP_HOST} ^([^.]+\.)?mydomain2\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mywebsitefolder2/
RewriteRule (.*) /mywebsitefolder2/$1 [L]
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]
I have a problem with subdomains I used to translate my website. With the 4 first lines of code, I've no problem, the sub var is passed fine (http://en.mywebsite.com/ gives http://mywebsite.com?sub=en). The 5th line is used to manage ads, once I click on the link like http://en.mywebsite.com/blue-chair-Vha6J.html the page isn't loaded and I stay on the home page. It should gives me something like http://mywebsite.com?sub=en&menu=ad&ad=Vha6J. Instead of this, it gives me http://mywebsite.com?sub=en.
RewriteBase /
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP_HOST} ^([a-z]+)\.mywebsite\.com$ [NC]
RewriteRule ^(.*) ?sub=%1 [NC,L]
RewriteRule ^([-a-z0-9]+)-([A-Za-z0-9]+)\.html$ ?sub=%1&menu=ad&ad=$2 [L]
[EDIT 1]
Ok, here is a bigger htaccess extract, included the modifications I made with the first answer, and I still have problems :
RewriteBase /
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} !&menu=.*
RewriteCond %{HTTP_HOST} ^([a-z]+)\.mywebsite\.com$ [NC]
RewriteRule ?sub=%1 [NC,L]
RewriteCond %{HTTP_HOST} ^([a-z]+)\.mywebsite\.com$ [NC]
RewriteRule ^cart$ ?sub=%1&menu=cart [L]
RewriteRule ^login$ ?sub=%1&menu=login [L]
########### AD
RewriteRule ^([-a-z0-9]+)-([A-Za-z0-9]+)\.html$ ?sub=%1&menu=ad&ad=$2 [L]
RewriteRule ^([-a-z0-9]+)-([A-Za-z0-9]+)\.html\/([A-Za-z0-9]+)$ ?sub=%1&menu=ad&ad=$2&secret=$3 [L]
########### ADS
RewriteRule ^all\/([-A-Za-z0-9]+)$ ?sub=%1&menu=ads§ion_name=$1 [L]
RewriteRule ^all\/([-A-Za-z0-9]+)\/([-A-Za-z0-9]+)$ ?sub=%1&menu=ads§ion_name=$1&category_name=$2 [L]
RewriteRule ^all\/([-A-Za-z0-9]+)\/([-A-Za-z0-9]+)\/([-A-Za-z0-9]+)$ ?sub=%1&menu=ads§ion_name=$1&category_name=$2&subCategory_name=$3 [L]
########### SHOPS
RewriteRule ^shops$ ?sub=%1&menu=shops [L]
########### SHOP
RewriteRule ^([-a-z0-9]+)$ ?sub=%1&menu=shop&shop=$1 [L]
RewriteRule ^([-a-z0-9]+)\/([-a-z0-9]+)$ ?sub=%1&menu=shop&shop=$1&category_name=$2 [L]
When I'm on the home page https://en.mywebsite.com/, the sub var "en" is passed fine. If I go on https://en.mywebsite.com/login for instance, the page is loaded, but the sub var "en" isn't passed.
Any idea ? Thanks.
the problem is that the url is Rewritten twice , the first time /sample-vg.html is rewritten correctly to ?sub=en&menu=ad&ad=fg, then in the second run the new url is been rewritten again to ?sub=en (L flag just stop the current run, not the entire process, take a look here)
there is also a problem with the second RewriteRule(undefined %1),
the following code should work as expected:
RewriteBase /
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} !&ad=.*
RewriteCond %{HTTP_HOST} ^([a-z]+)\.mywebsite\.com$ [NC]
RewriteRule ^(.*) ?sub=%1 [NC,L]
RewriteCond %{HTTP_HOST} ^([a-z]+)\.mywebsite\.com$ [NC]
RewriteRule ^([-a-z0-9]+)-([A-Za-z0-9]+)\.html$ ?sub=%1&menu=ad&ad=$2 [L]
Let's say short.com is the short domain and long.com is the long domain
updated:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.long.com
RewriteRule (.*) http://long.com/ [R=301]
RewriteCond %{HTTP_HOST} ^short\.li$ [NC]
RewriteCond %{REQUEST_URI} !^/redirect
RewriteRule ^(.*)$ /redirect?short=$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)short\.li$
RewriteRule ^$ http://long.com/ [L,R=301]
both domains point to that root directory. When I type short.li I end up on long.com/?l=
how did I manage to screw up like that?^^
Try this in your htaccess file :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)short\.com$
RewriteRule ^$ http://www.long.com/ [L,QSA,R=301]
Remove RewriteEngine on if it is already there
I think you might want something along the lines of this:
RewriteEngine on
RewriteCond {REQUEST_URI} !/
RewriteCond {HTTP_HOST} short.com
RewriteRule ^(.*) http://long.com/redirect.php?short=$1 [L,R=301]
RewriteCond {REQUEST_URI} /
RewriteCond {HTTP_HOST} short.com
RewriteRule ^(.*) http://long.com/ [L,R=301]
Not sure if the regex is needed on that last one or not, but something like this should work
I have this code in my .htaccess to handle subdomains (http://foo.mydomain.com)
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com [NC]
RewriteRule ^(.*)$ filter.php?type=country&country=%1 [L]
The problem is what do i have to do if for example someone puts a link like this:
http://foo.mydomain.com/bar
i want to redirect this to another page different from the filter.php (say otherpage.php) from the previous code, i tried the next code but it isn't working
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com/([^/]+) [NC]
RewriteRule ^(.*)$ otherpage.php?type=country&country=%1 [L]
neither the next one:
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com [NC]
RewriteCond %{REQUEST_URI} ^/([^/]+)
RewriteRule ^(.*)$ otherpage.php?pname=$1 [L]
The link is always resolved with the first rule i wrote.
Add a condition to your first group that checks for an empty REQUEST_FILENAME. This will prevent it from matching when there's something other than a bare domain name.
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com [NC]
RewriteCond %{REQUEST_FILENAME} ^/?$
RewriteRule ^(.*)$ filter.php?type=country&country=%1 [L]