简体   繁体   中英

Regular expression for Apache url rewrite .htaccess

I would like to rewrite this:

www.mywebsite.com/i.php?photo=1234

to

www.mywebsite.com/i/1234

Can anyone help me with the right regular expression for .htaccess

Try this:

RewriteRule ^i/([0-9]+)/?$     i.php?photo=$1     [NC,L]

Remember to turn the rewrite engine on before you declare the rule:

RewriteEngine On

The first thing you need to do is make sure Multiviews is turned off, since you have a URI that is /i/ as well as a script that is called /i.php . Multiviews and mod_negotiation will bypass mod_rewrite and your rules will get skipped.

Options +FollowSymlinks -Multiviews
RewriteEngine On

RewriteRule ^i/([0-9]+)/?$ /i.php?photo=$1 [L,QSA]

And for redirecting links that are outside of your control:

RewriteCond %{THE_REQUEST} \ /+i\.php\?photo=([^&\ ]+)
RewriteRule ^ /i/%1/ [L,R=301]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM