Currently, I'm having an url system like this by using the htaccess:
- www.domain.com/member/username
- www.domain.com/member/username/contact
- www.domain.com/member/user/album/title-of-the-album/albumID
.. something like that
Here is my htaccess, and it worked perfectly.
RewriteRule ^member/([a-zA-Z0-9_-]+)$ member.php?username=$1
RewriteRule ^member/([a-zA-Z0-9_-]+)/contact$ contact.php?username=$1
RewriteRule ^member/([a-zA-Z0-9_-]+)/album/([a-zA-Z0-9_-]+)/([0-9]+)$ album.php?username=$1&title=$2&album_id=$3
Now I want to setup a dynamic subdomain system for the user, likes "username.domain.com"
So I decided to used the below htaccess:
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC]
RewriteRule ^(.*)$ www.domain.com/member/%1 [L]
But this redirect the user to their old domain "www.domain.com/member/username" instead of "www.domain.com/member/username"
I want to keep the user stay in "username.domain.com" (no url change in the address bar).
If possible, is there any chance to keep the same structure for other new url, such as when I type:
"username.domain.com/contact" will load the content of "www.domain.com/member/username/contact" (no url change in the address bar)
"username.domain.com/album/title-of-the-album/albumID" will load the content of "www.domain.com/member/username/album/title-of-the-album/albumID" (no url change in the address bar)
Please help !!
Try:
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC]
RewriteRule ^/?$ /member.php?username=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC]
RewriteRule ^/?contact$ /contact.php?username=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC]
RewriteRule ^/?album/([a-zA-Z0-9_-]+)/([0-9]+)$ /album.php?username=%1&title=$1&album_id=$2 [L]
Related
I need to redirect a page (not whole domain!) to a specific other page that's on another domain, with htaccess. I tried a couple of things but can't make it to work. Can anybody help me out?
I tried this one:
#RewriteEngine On
#RewriteCond %{HTTP_HOST} ^domain.com/example [NC]
#RewriteRule ^(.*) https://otherdomain.com/page [P]
and this one:
#RewriteEngine on
#RewriteCond %{HTTP_HOST} ^domain.com/example/$ [NC,OR]
#RewriteCond %{HTTP_HOST} ^www.domain.com/example/$ [NC]
#RewriteRule ^(.*)$ http://www.mainpage.com/pagename/$1 [L,R=301,NC]
The page that should be redirected is /example
domain.com/example >>> should redirect to: https://otherdomain.com/page
The page that should be directed is accessible with and without www. in front of it, not sure if this is relevant.
Many thanks!
Your rule doesn't work because it has an error in the RewriteCond
#RewriteCond %{HTTP_HOST} ^domain.com/example/$ [NC,OR]
This condition will never be met as %{HTTP_HOST} variabe only contains the host header ie. example.com and not the URI string /example/ . You can test URI string using %{REQUEST_URI} variable in a separate RewriteCond .
This should work for you
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} /example [NC]
RewriteRule ^ https://otherdomain.com/page [R,L]
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 the following set up for an old domain, which works really well to mapping the old pages to the ones on the new domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^oldurl.org.uk [NC,OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.org.uk [NC]
RewriteRule ^(.*)$ http://www.newurl.co.uk/$1 [L,R=301,NC]
The problem is I need to redirect the homepage to a specific page on the new URL, keeping the above in tact. Whats the best way to do this?
Thanks in advance!!
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?oldurl\.org\.uk$ [NC]
RewriteRule ^$ http://www.newurl.co.uk/specificPage [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?oldurl\.org\.uk$ [NC]
RewriteRule ^(.+)$ http://www.newurl.co.uk/$1 [L,R=301]
I am stuck on the follow htaccess situation.
I want to redirect pages to subdomains.
Url example right now;
http://domain.tld/user/foo
I need to redirect this to;
http://foo.domain.tld
The subdomain is genereated by wildcard.
I tried the follow without succes;
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.tld [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.tld?$
RewriteRule (.*) s/index.php?user=%1 [NC,QSA]
RewriteRule ^user/(.*) (.*) [R=301,L]
Regards,
Nick
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.tld [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.tld?$
RewriteRule (.*) s/index.php?user=%1 [NC,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.tld [NC]
RewriteRule ^user/([a-zA-Z0-9-.]+) http://$1.domain.tld/ [R=301,L]
I have set up virtual subdomain. and i want to make htaccess to achive the following flow
For
www.domain.com
it should call the index file but for the subdomain
abc.domain.com
The rewrite rule should be like
RewriteRule www.domain.com/index.php?var=abc
i mean it should (in HTACCESS) pass the subdomain to the index file as an argument
and for the Other file requests like
abc.domain.com/file.php
The subdomain should be rewritten like www.domain.com/file.php?var=abc
I mean the Rewrite rule like
RewriteRule www.domain.com/file.php?var=abc
This would be how you could do them on an individual basis.
RewriteCond %{HTTP_HOST} ^abc\.domain\.com$ [NC]
RewriteRule (.*)$ http://www.domain.com/file.php?var=abc [L]
RewriteCond %{HTTP_HOST} ^def\.domain\.com$ [NC]
RewriteRule (.*)$ http://www.domain.com/file.php?var=def [L]
The solution is something like this
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://www.domain.com/index.php?subdomain=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?%{QUERY_STRING}&subdomain=%1 [L]