htaccess “silent” redirect

2019-09-05 08:25发布

i am having a bit trouble on my htaccess and maybe you could help me.
I want to redirect my requests like this:

www.fakedomain.mydomain.com/dev > www.mydomain.com/dev/events/fakedomain.php

The HTTP_HOST would be fakedomain.mydomain.com i guess, but the dev part is important too. My last try was :

RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.mydomain\.com [NC]
RewriteRule  ^(.*)$ /dev/events/%2 [L]

This redirect is shown at the top of the browser, something I want to avoid.
Thank you for your time

1条回答
Juvenile、少年°
2楼-- · 2019-09-05 09:24

If you are using your DOCROOT/.htaccess for this, you should use:

RewriteEngine On
RewriteBase   /

RewriteCond %{HTTP_HOST}         ^(www\.)?(\w+)\.mydomain\.com  [NC]
RewriteRule ^dev/(?!events/)     dev/events/%2.php              [L]

The (?!events/) bit of the regexp is called a lookahead assertion and this prevents the rule matching /dev/events/something and hence prevents a rewrite loop.

If you are using your DOCROOT/dev/.htaccess for this, you should use:

RewriteEngine On
RewriteBase   /dev

RewriteCond %{HTTP_HOST}      ^(www\.)?(\w+)\.mydomain\.com  [NC]
RewriteRule ^(?!events/)      events/%2.php                  [L]

The (?!events/) bit of the regexp is called a lookahead assertion and this prevents the rule matching /dev/events/something and hence prevents a rewrite loop.

查看更多
登录 后发表回答