Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
873 views
in Technique[技术] by (71.8m points)

mod rewrite - PHP .htaccess -> pretty url (in reverse)

I know how to make URL's rewrite, for example: www.example.com/index.php?id=1&cat=3 to www.example.com/1/3/ (or whatever). I know that.

What I don't know is how on earth to change my whole links in all pages to link to pretty URL's. All my site's links are old fashion (<a href="index.php?id=1&cat=2">) and there are many.

I`m asking if someone has an idea or know how to automaticaly redirect to that pretty url if the user click on index.php?id=1. (Almost like this site Stackoverflow if you change title in the url).

So my presumtions are...

  1. Use .htaccess to read the index.php?id=1&cat=2 to rewrite index/1/3 that itself interprets again (strange)

  2. a php file to do the redirects that htaccess rewrites back to original...

Conclusion: change <a href="index.php?id=1&....."> automaticaly to index/1/2


SOLVED

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

##################################
# This turns index.php?id=1&cat=2 into index/1/2 and then back 'transparent' into    index.php?id=1&cat=2 if you have old fashioned
# links in your site and don't want to change them :)


# Avoid mod_rewrite infinite loops 
# This is critical if you want to use this code

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

# Hard-rewrite ("[R]") to "friendly" URL.
# Needs RewriteCond to match original querystring.
# Uses "?" in target to remove original querystring,
#   and "%n" backrefs to move its components.
# Target must be a full path as it's a hard-rewrite.
RewriteCond %{QUERY_STRING} ^id=(d+)&cat=(d+)$
RewriteRule ^index.php$ http://localhost/index/%1/%2/? [L,R]

# Soft-rewrite from "friendly" URL to "real" URL.
# Transparent to browser.
# Won't re-trigger the above rewrite, though I'm
#   not really sure why! The order of the rules
#   doesn't seem to make a difference.
RewriteRule ^index/(d+)/(d+)/$ index.php?id=$1&cat=$2 [L]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
RewriteEngine on

# Prevents browser looping, which does seem
#   to occur in some specific scenarios. Can't
#   explain the mechanics of this problem in
#   detail, but there we go.
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

# Hard-rewrite ("[R]") to "friendly" URL.
# Needs RewriteCond to match original querystring.
# Uses "?" in target to remove original querystring,
#   and "%n" backrefs to move its components.
# Target must be a full path as it's a hard-rewrite.
RewriteCond %{QUERY_STRING} ^id=(d+)&cat=(d+)$
RewriteRule ^index.php$ http://example.com/index/%1/%2/? [L,R]

# Soft-rewrite from "friendly" URL to "real" URL.
# Transparent to browser.
RewriteRule ^index/(d+)/(d+)/$ /index.php?id=$1&cat=$2

Of course, ideally, you'd just fix your links, and then you'd only require the soft-rewrite. :)

Tested with Apache/2.2.3. I think I made up the terms "hard-rewrite" and "soft-rewrite".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...