简体   繁体   中英

Simple rewrite rule for mod_rewrite

I am in need of writing a simple mod_rewrite rule, as follows:

something.php ==> somehting (just get rid of the .php)
something_else.php ==> something/else (replace _ with /)

So, for example it would be like this:

www.mysite.com/something.php (would be rewritten as) www.mysite.com/something
www.mysite.com/something_else.php (would be rewritten as) www.mysite.com/something/else

As I am not familiar with regular expressions, I thought it would be simpler to ask someone who can do this in 5s rather than spend days (weeks? :P) learning regular expressions and mod_rewrite.

Thanks:)

Try these rules in .htaccess file:

Options +FollowSymlinks -MultiViews
RewriteEngine on

RewriteRule ^([^_]+)_([^_]+)\.php/?$ /$1/$2 [R=301,L]
RewriteRule ^([^.]+)\.php/?$ /$1 [R=301,L]

This will do external redirect , in case you don't want that then remove R=301, flag from above.

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