开发者

Using htaccess to redirect host urls but not when they contain paths or parameters

开发者 https://www.devze.com 2023-04-10 09:21 出处:网络
What I would like to do is use htaccess to redirect (301, Permanent Redirect) simple host only urls to another domain but only when the url contains no path or parameters.

What I would like to do is use htaccess to redirect (301, Permanent Redirect) simple host only urls to another domain but only when the url contains no path or parameters.

For example:

http://www.mydomain.开发者_开发百科com should redirect to http://www.myotherdomain.com and http://mydomain.com should redirect to http://myotherdomain.com

(which I have found plenty of examples for)

but a url like: http://www.mydomain.com/someimage.jpg or http://mydomain.com/js/coolJsScript.js should not be redirected at all.

Any help would be greatly appreciated!

Thanks!


I think this rule will match incoming URLs without any path:

RewriteRule ^$ http://othersite.com/ [L,R=301]

You haven't stated exactly what you are trying to do here, but there might be an alternative, which is to check to see if a file exists, and redirect to your other site if it doesn't exist. This test would be something like

# Check if file exists
RewriteCond %{REQUEST_URI} -f

# it exists, so stop processing
RewriteRule .* - [L]

# Redirect everything else
RewriteRule (.*) http://othersite.com/$1 [L,R=301]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号