stuck on htaccess rewrite url - .htaccess

i've been stuck on this for a few weeks, i think i'm close now, but i'm stuck on syntax for htcaccess..
here's what i'm trying to do: user types in site.com and is redirected to subdomain.wordpresshost.com; however the address bar still says site.com (i also want to keep anything after this point such as/blog.html etc)
nameserver is all set, now i'm just rewriting urls... heres the best code i have come up with.. it's just not working
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.wordpresshost.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^site.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.site.com$ [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [L,R=301]
This code successfully changes subdomain.wordpresshost.com to site.com; however, it returns an error stating either 'server unavailable' or 'too many redirects'
I think i've hit my head against the keyboard so that I'm just making static noise, so i'd appreciate any help!

The last RewriteCond is unnecessary and is creating the redirection loop - www.site.com is rewritten to www.site.com… maybe you meant the non-www. Either way, it needs to be removed.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.wordpresshost.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^site.com$ [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [L,R=301]

Related

Htaccess redirect subdomains to a specific page without changing the link

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.

Hide subfolder for subdomain

so my subdomain currently goes to the same root folder as my domain. I would like to accomplish the following:
sub.domain.com => sub.domain.com/sub/
with the /sub/ portion invisible to the user.
I do not want to have the following happen:
sub.domain.com => domain.com/sub/
Is there any way I can accomplish this without redirection and without getting stuck in an endless loop? I have this currently and I don't think it's working as it doesn't mask the /sub part:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/sub
RewriteRule .* http://sub.domain.com/sub[L]
I've looked endlessly online for something that accomplishes this but can't seem to find it...
I figured it out through a bunch of trial and error:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/sub
RewriteRule ^(.*)$ sub/$1 [L]

Subdomain with subfolder gets a 404 error

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.

.htaccess, Rewriterule not working as i want

I have this link: http://www.domain.com.mk/lajmi.php?id=2790,
and i want to change it to http://www.domain.com.mk/lajmi/2790
With this code I can change the link to /lajmi/2790 but i get 404 error.
I mean i get the link
http://www.domain.com.mk/lajmi/2790, but it has 404 error (i dont se the content)
This is my code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com\.mk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\.mk$
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^lajmi\.php$ http://domain.com.mk/lajmi/%1? [R=302,L]
What I am doing wrong ?
Try this one :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteCond %{QUERY_STRING} ^id=(\d*)$
RewriteRule ^lajmi\.php$ http://domain.com.mk/lajmi/%1? [R=302,L]
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1&r=0 [L]
(the &r=0 in the final rule is for not getting an infinite loop)
Single direction rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1 [L,QSA]
This means that every uri of kind /lajmi/2790 will be passed to /lajmi.php?id=2790 in a sub-request.
However, in this case, if the user hits /lajmi.php?id=2790 by himself, then this is the url he will see in the browser, not the "beautified one".
Bi-directional rewrite:
RewriteEngine On
RewriteBase /
; Redirect lajmi.php?id=2790 to a beutified version, but only if not in sub-request!
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteCond %{IS_SUBREQ} !=true
RewriteCond %{QUERY_STRING} ^id=(\d*)$
RewriteRule ^lajmi\.php$ lajmi/%1 [R=301,L]
; Make the beutified uri be actually served by lajmi.php
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1 [L]
Here, an additional RewriteCond was added to the first rule checking that this is not a sub-request, to ensure that the rules do not loop.
You can pick which way you like, but the first approach is enough if you build the links in your HTML in the 'beautified' way already (no need to redirect the browser twice just to see the page).

complex mod_rewrite. Any idea?

I want to :
- switch from http to https if http is used
- redirect the subdomain to index?o=subdomain except www
- redirection the subdirectory to index?u=user
Example :
http://www.mydomain.com will be redirected to https://www.mydomain.com
http://subdomain.mydomain.com will be redirected to https://www.mydomain.com/index?o=subdomain
https://subdomain.mydomain.com will be redirected to https://www.mydomain.com/index?o=subdomain
http://subdomain.mydomain.com/user will be redirected to https://www.mydomain.com/index?o=subdomain&u=user
https://subdomain.mydomain.com/user will be redirected to https://www.mydomain.com/index?o=subdomain&u=user
Is mod_Rewrite the best to do that ? Any idea ?
Thanks in advance
I don't have time to test it right now, but you can try this and see if it works. There may be some potential for some things to go wrong, so if you have trouble with it I'd be happy to work out any kinks later. Also, I think that I covered everything you wanted to do, but let me know if I left something out.
RewriteEngine On
# Force redirect to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}/$0 [R=301,L]
Edit: I've updated the ruleset below. I thought about your question though, and aren't you going to have issues attempting to serve up your subdomains over TLS/SSL? That aside, one of the following should do what you want (without errors this time, I hope):
If you wanted internal redirection:
RewriteCond %{HTTP_HOST} !=mydomain.com
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{REQUEST_URI} !^/index
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^([^\.]+)[^/]*/([^/]+)?
RewriteCond %1&u=%2 ^([^&]+)(&u=.+)?
RewriteRule ^.*$ /index?o=%1%2
If you wanted external redirection:
RewriteCond %{HTTP_HOST} !=mydomain.com
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{REQUEST_URI} !^/index
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^([^\.]+)[^/]*/([^/]+)?
RewriteCond %1&u=%2 ^([^&]+)(&u=.+)?
RewriteRule ^.*$ https://www.mydomain.com/index?o=%1%2 [R=301,L]

Resources