开发者

Htaccess RewriteRule trouble - How to redirect to a "re-written" url on the server?

I have the following rewrite rule which is working fine:

RewriteRule ^([a-zA-Z\-]+)[/]?$ apply/?survey_folder=$1

which turns this: address.com/folder1/

into this: 开发者_如何转开发address.com/apply/?survey_folder=folder1

The problem is that I can't figure out a way to use a redirect to the rewritten URL. Using cfml as an example, (cflocation url="http://www.address.com/folder1/") throws an error because, of course, the folder "folder1" doesn't actually exist on the server.

I can get the user redirected to the correct page by using /apply/?survey_folder=folder1, but that defeats the purpose of having the rewrite rule at all.

Is there any way to redirect to a URL rewritten by htaccess? I'm new at RewriteRule.


Add the [L] flag to the rule otherwise you're in danger of infinite loops and other problems.

The redirect you need is incredibly simple.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /apply/(index\.php)?\?survey_folder=([\ ]+)\ HTTP/
RewriteRule ^apply/(index\.php)? http://www.example.com/%1 [R=301,L]

RewriteRule ^([A-Z-]+)$ /apply/?survey_folder=$1 [NC,L]

Do not let both URLs with trailing slash and URLs without trailing slash resolve to the same content. The URL for a page does not have a trailing slash. Add another preceding redirect to strip trailing slashes in that case.


Have you tried setting RewriteBase? Its purpose is to map the URL to the physical location on the server. I'm not completely sure this is the problem you are having, but it does sound like a possibility.

http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritebase

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜