Subdomain with subfolder gets a 404 error - .htaccess

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.

Related

htaccess redirect (multidomain multilanguage) subfolder wildcard

I have a typo3 installation with multidomain and multilanguage, where not every language is setup to every domain.
Languages are de/fr/en/pt/es/cn/
www.example.de can be de/en/fr but not
www.example.de pt/es/cn
now I have messed up sth. and google has indexed loads of wrong urls e.g.
www.example.de/pt/
www.example.de/es/
www.example.de/cn/
they point to languages that are not set for this domain
I am fiddling around in the htaccess to redirect 301 the wrong urls with wildcards(?) to the .tld
I am looking for a solution redirect eg.
www.example.de/pt/* to www.example.de/
www.example.de/es/* to www.example.de/
www.example.de/cn/* to www.example.de/
where the * should represent the complete sting/path following the language parameter.
and of cause the same procedure for the .com domain
www.example.com/fr/* to www.example.com/
www.example.com/de/* to www.example.com/
I searched the web up and down but nothing I tried works.
any help would highly apreciated.
a tiny step further
RewriteCond %{HTTP_HOST} ^www.example.de
RewriteRule ^cn/(.*)$ http://www.example.de/ [L,R=301]
RewriteRule ^pt/(.*)$ http://www.example.de/ [L,R=301]
RewriteRule ^es/(.*)$ http://www.example.de/ [L,R=301]
this seems to work
and now for the second domain as I need to differentiate between .com and .de
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^de/(.*)$ http://www.example.com/ [L,R=301]
RewriteRule ^fr/(.*)$ http://www.example.com/ [L,R=301]
this now breaks www.example.de/fr/whatever and redirects it to www.example.com as well.
so it looks like the first condition is matching and the the last rule is applied for french.
how can I limit the rules assigning them only to the appropriate domain conditions?
ok looks like every condition and respective rule needs to be written in a single line an repeated
RewriteCond %{HTTP_HOST} ^www.example.de
RewriteRule ^cn/(.*)$ http://www.example.de/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.de
RewriteRule ^pt/(.*)$ http://www.example.de/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.de
RewriteRule ^es/(.*)$ http://www.example.de/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^de/(.*)$ http://www.example.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^fr/(.*)$ http://www.example.com/ [L,R=301]
this seems to work.
any better solution is highly appreciated

Why does the following htacces directive not work as expected?

I have the following redirects:
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^id=409$
RewriteRule ^(.*)$ http://www.domain.eu/index.php\?id=4 [R=301,L]
RewriteCond %{REQUEST_URI} foo_Bar\.pdf$ [NC]
RewriteRule ^(.*)$ http://www.domain.eu/index.php\?id=4 [R=301,L]
The first one works fine.
But the second one is not directing, when I open this url: domain.eu/fileadmin/images/foo_Bar.pdf (showing an 404 instead, thats why I want to redirect).
I've also tried to add a ^(.*) before the filename, but it doesnt work either. I've even tried to enter full url as the request uri, no luck. What am I missing?
Thanks
It will redirect www.yourdomain.com/index.php?id=409 to http://www.domain.eu/index.php?id=4
And the second will redirect if your site address ends with .foo_Bar.pdf redirects to http://www.domain.eu/index.php?id=4
You need to change the second one to:
RewriteCond %{REQUEST_URI} foo_Bar\.pdf$ [NC]
RewriteRule ^(.*)$ http://www.domain.eu/index.php\?id=4 [R=301,L]

.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).

HtAccess Rewrite works with [R] but fails with [L]

So, I am trying to make: sub.example.com/page rewrite to www.example.com/sub/page
I have this code which does work (note last character):
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).example.com [NC]
RewriteRule ^(.*) http://www.example.com/%1/$1 [R]
And this code that does not:
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).example.com [NC]
RewriteRule ^(.*) http://www.example.com/%1/$1 [L]
I want the url to remain as the user typed it in, but this seems to produce a 500 error. I've enabled logging to try to assist, but it doesn't provide any clues. Is this possible?
Try changing the last Rule to
RewriteRule ^(.*) http://www.example.com/%1/$1 [P]
If you list your directory structure to show how sub.example.com and www.example.com are related, this may also be possible via a rewrite vs the more expensive (slower) proxy solution above.

Redirect/Subdomain with .htaccess

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]

Resources