开发者

Creating a simple rule to rewrite a friendly URL to the physical URL

开发者 https://www.devze.com 2023-04-09 06:48 出处:网络
I cannot wrap my head around URL rewriting.What I want to do seems very simple but I am having problems getting the results I want.

I cannot wrap my head around URL rewriting. What I want to do seems very simple but I am having problems getting the results I want.

I would like allow users to type www.mysite.com/search/real with a开发者_JAVA百科n optional / at the end. This would take them to www.mysite.com/content/search_real_property.asp

That's it. Here is the rule I have right now. The problem with this is it will keep stacking.

RewriteRule ^(search) content/search_real_property.asp

So this would work /search/real but so would search/real/search/real/search/real/ and others.


Assuming there are no other issues, you've turned the rewrite engine on (RewriteEngine On) and that you're either adding the rewrite in httpd-vhosts.conf or an .htaccess file in the root of the web tree (so that any path issues are resolved)... then the issue is merely one of Regular Expression pattern matching. Though I'm a bit perplexed by ASP running on what appears to be an Apache server (assuming this IS mod rewrite we're talking about).

So, all you really want is to terminate the match - something like:

RewriteEngine On
RewriteRule ^search/real/?$ /content/search_real_property.asp

That will fix it to /search/real (with or without a trailing slash, the ? means match the preceding character 0 or 1 times) to /content/search_real_property.asp. As the $ sign denotes the line terminator (EOL effectively) there must be nothing after "real" (except perhaps that 1 forward slash).

For greater flexibility you might want to look at what you can actually do with regular expressions, for instance...

RewriteEngine On
RewriteRule ^search/([^/]*)/?$ /content/search_real_property.asp?query=$1

Which would allow you to take any string and pass it in the address bar as a variable called query (Request.QueryString('query') IIRC).

Try: http://www.regular-expressions.info/ for more info.

0

精彩评论

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

关注公众号