简体   繁体   中英

Vanity URLs with .htaccess

I need to implement vanity urls and this is my .htaccess file

# check if mod_rewrite is present
<IfModule mod_rewrite.c>

  #turns it on
  RewriteEngine on

  #if the requested url isn't a file or a dir
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d

  #if the request does not end with .gif, .png or .jpg
  RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC]

  #process book.php, no matter what was in the url
  RewriteRule ^(.*)$ /book.php?book_id=$1 [L,QSA]

</IfModule>

This translation works correctly.

www.mydomain.com/foo -> www.mydomain.com/book.php?book_id=foo

Now i need a more specific condition:

www.mydomain.com/foo/readers -> www.mydomain.com/book.php?book_id=foo&readers=1

While readers doesn't change, foo is a random string and i'd like to check this specific url

www.mydomain.com/foo/readers -> valid
www.mydomain.com/foo/readers?q=hello -> valid
www.mydomain.com/foo/reader -> invalid
www.mydomain.com/foo/readers/bar -> invalid
www.mydomain.com/foo/bar/readers -> invalid

#I tried many solutions like this but i can't get the desired result
RewriteRule ^(.*)[/]readers /book.php?book_id=$1&readers=1 [L,QSA]

您需要在规则中使用锚,并使斜杠为可选:

RewriteRule ^([^/]+)/readers/?$ /book.php?book_id=$1&readers=1 [L,QSA,NC]

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