Hello I have a problem with wildcard subdomains with htaccess and mod_rewrite.
I want www.domain.com/index.php?foo=x&bar=y translated to x-y.domain.com
My htaccess looks like this
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteCond %{HTTP_HOST} ^(.*?)-(.*?).domain.com$
RewriteRule (.*) /index.php?foo=%1&bar=%2
This work but with one minor issue, if I go to x-y.domain.com. Its show www.domain.com, but if I go to x-y.domain.com/z ( 'z' can be every value, even 'asdfasdfasdfasdfasdf' ) it shows the right page, my application doesnt use z for anything its just there for no reason, but its necessary to show the right page.
Solution
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*?)-(.*?).domain.com$ [NC]
RewriteRule (.*) index.php?foo=%1&bar=%2 [NC]
I dont know why it works but it works
Related
I am working on a php script on which i will generate links with random subdomains.For example : x.domain.com sius.domain.com x5-.domain.com and so on. In fact these subdomains doesn't really exist what i want is that when user goes to any link (RANDOM).domain.com it shows the content of domain.com/result.php?rand=(RANDOM).
PS:I considered (RANDOM) as the variable. and i want to exclude www.
I tried this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^ http://domain.com/result.php [L,R]
But nothing seems to work. Can anyone help ?
Try with:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteRule ^ /result.php?rand=%1 [L,QSA]
You can't redirect (http://... and [R]) without link change. With this code you rewrite without redirection.
I have multiple subdomains that are working with a redirect, but bad for SEO
RewriteRule ^([a-zA-Z0-9&:_-]+)/actions$ http://$1.actions.mydomain.nl [R=301,L]
I want to redirecting to:
http://$1.mydomain.nl/actions
but here i get a 404 error.
How to make the right rule for this?
I assume that your rule looks like this:
RewriteRule ^([a-zA-Z0-9&:_-]+)/actions$ http://$1.mydomain.nl/actions [R=301,L]
So the destination becomes http://subdomain-name.mydomain.nl/actions. So the first thing you need is to make sure a DNS entry is setup to point subdomain-name.mydomain.nl to the right IP address/server. Then on the actual server you need to make sure that the requests are routed to the right place. So assuming that the actual resource is /subdomain-name/actions, you'd need:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain.nl$ [NC]
RewriteRule ^actions$ /%1/actions [L]
This is the complete code i have:
RewriteCond %{HTTP_HOST} !^www.
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9&:_-]+).actions.mydomain.nl$ [NC]
RewriteRule ^$ file.php?seo_naam=%1 [L,QSA]
This is working,but multiple(2 level) subdomains are not a good thing for seo.
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9&:_-]+).actions.mydomain.nl$ [NC]
but i want to change this to:
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9&:_-]+).mydomain.nl\actions$ [NC]
does not work
the thing is that the file.php?seo_naam=%1 should be loaded otherwise it will not work.
I want to redirect an html page which is a contact form like this www.abc.com/contact.html to a different domain but without www like this xyz.com/contact.html.
I am having 2 domains with same data (Just the domain names are different). The form on xyz.com is working perfectly fine, but the form on abc.com is not working even though the code on both domains is 100% same.
Simply put the following lines in your .htaccess file.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www?.abc.com [NC]
RewriteRule ^contact\.html$ http://xyz.com%{REQUEST_URI} [R=301,L]
EDIT:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.abc.com [NC]
RewriteRule ^contact\.html$ http://xyz.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^abc.com [NC]
RewriteRule ^contact\.html$ http://xyz.com%{REQUEST_URI} [R=301,L]
I am using the following code to redirect wildcard subdomains (*.domain.com) to their coresponding folder in /users and redirect direct requests to the /users folder to the subdomain version:
Protect Direct Access to Wildcard Domain Folders:
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^users/([a-z0-9\-_\.]+)/?(.*)$ http://$1.domain.com/$2 [QSA,NC,R,L]
Handle Wildcard Subdomain Requests:
RewriteCond %{REQUEST_URI} !^/users/ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %1 !=www [NC]
RewriteRule ^(.*)$ /users/%1/$1 [L]
This code works well enough, however there are two problems that I can't seem to fix.
The below scenario seems to happen because there isn't a trailing slash on the requesting URI:
username.domain.com/sub1 => username.domain.com/users/username/sub1
username.domain.com/sub1/ => username.domain.com/sub1/
The users directory can still be accessed directly by using a subdomain:
username.domain.com/users/username/sub1 => Works and shouldn't
I'm at a loss and would really appreciate if anyone has any ideas.
Thank you!
For the problem 2, I think your first protection rule just needs to redirect all subdomains. It's redirecting www, but lets username.domain.com come through as-is.
This will redirect any direct access request to the users path.
RewriteCond %{HTTP_HOST} ^.+\.domain\.com$ [NC]
RewriteRule ^users/([a-z0-9\-_\.]+)/?(.*)$ http://$1.domain.com/$2 [QSA,NC,R,L]
I think it can be a little simpler by just looking for any host ending in domain.com (which would even handle no subdomain, just domain.com) (I didn't test this....)
RewriteCond %{HTTP_HOST} domain\.com$ [NC]
RewriteRule ^users/([a-z0-9\-_\.]+)/?(.*)$ http://$1.domain.com/$2 [QSA,NC,R,L]
For problem 1, I'm stumped too, sorry. It's acting like the trailing slash is failing the rules, so it falls through as-is. But I would expect it to do as you want it to:
username.domain.com/sub1/ => username.domain.com/users/username/sub1/
Perhaps try the %{REQUEST_URI} tag, instead of trying to capture .*. I don't see the difference, but maybe it'll help.
RewriteCond %{REQUEST_URI} !^/users/ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %1 !=www [NC]
RewriteRule .* /users/%1%{REQUEST_URI} [L]
What I have currently:
A wildcard DNS record. So that every subdomain points to www.galop.gr
A dynamically (php) generated .htaccess file where I append the following code for every subdomain I want working:
RewriteCond %{HTTP_HOST} ^fractalbit.galop.gr$ [OR]
RewriteCond %{HTTP_HOST} ^www.fractalbit.galop.gr$
RewriteRule ^/?$ http://www.galop.gr/index.php?user=1 [R=301]
Everything is working fine, I just want something more if it is possible.
Right now, if someone enters fractalbit.galop.gr will be redirected to http://www.galop.gr/index.php?user=1
Is it possible to do this BUT keep fractalbit.galop.gr to the address bar of the browser?
[R=301] means: do a redirection, and since you're using an absolute address it will also force a redirection, i would just change the rules to this:
RewriteCond %{HTTP_HOST} ^fractalbit.galop.gr$ [OR]
RewriteCond %{HTTP_HOST} ^www.fractalbit.galop.gr$
RewriteRule ^/?$ /index.php?user=1 [L]