开发者

Mod_rewrite-multiple pages

开发者 https://www.devze.com 2022-12-10 19:25 出处:网络
I have two URLs that I would like to rewrite: /artist.php?name=x=> /x /albums.php?title=y => /y This is what I put in the .htaccess file:

I have two URLs that I would like to rewrite:

  1. /artist.php?name=x => /x

  2. /albums.php?title=y => /y

This is what I put in the .htaccess file:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ artist.php?name=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ artist.php?name=$1
RewriteRule ^([a-zA-Z0-9_-]+)$ albums.php?name=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ albums.php?name=$1

How should I edit the file so th开发者_高级运维at the albums.php's URL would be executed as well? (Only the first one is working.) Thanks for your help.


You have duplicate patterns for both artist and album, so they can't possibly both work! Not with just mod_rewrite anyway. (Your web app might be able to check for the existence of an artist or album with the supplied name parameter, and decide which one is intended, but that's beyond the scope of a simple mod_rewrite example.)

There needs to be something else in the URL to distinguish between artist and album, for example:

RewriteRule ^/artist/([a-zA-Z0-9_-]+)/?$ artist.php?name=$1
RewriteRule ^/album/([a-zA-Z0-9_-]+)/?$ albums.php?name=$1

Notice also that I shortened your 4 rewrite rules to 2, using /? (? is a quantifier meaning "zero or one") to allow the URL with or without a trailing slash. But in my opinion it would be altogether better to choose one or the other, to avoid having 2 URLs for the same page.


RewriteRule ^/artist/([a-zA-Z0-9_-]+)/?$ artist.php?name=$1 [L]
RewriteRule ^/album/([a-zA-Z0-9_-]+)/?$ albums.php?name=$1 [L]
0

精彩评论

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