简体   繁体   中英

.htaccess Pretty URL rewrite for PHP pages in a subfolder?

I've got the following directory setup:

http://www.mysite.com/public/

I'm using the following rewrite to remove the 'public' folder from visible URLs:

#rewrite the URL to display the subdirectory as the main directory for all links
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com
RewriteCond %{REQUEST_URI} !^/public
Rewriterule ^(.*)$ /public/$1 [L]

This works great, and redirects everything correctly. However, I also want to rewrite some of the dynamic pages that live in the 'public' subfolder as well, but am having trouble getting any of the rewrites I've found to work in conjunction with the above rule.

For example, with the above subdirectory rewrite rule in place, going to a URL like:

http://www.mysite.com/item.php?id=1&name=item_name

...should be rewritten to something like:

http://www.mysite.com/items/item_name

Thoughts?

Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /

#rewrite the URL to display the subdirectory as the main directory for all links
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
Rewriterule ^(?!public/|prints/)(.*)$ /public/$1 [L,NC]

RewriteCond %{THE_REQUEST} /*item\.php\?id=([^&]*)&name=([^&]*)\s [NC]
Rewriterule ^ /prints/%1/%2? [R,L,NC]

Rewriterule ^prints/([^/]*)/([^/]*)/?$ public/item.php?id=$1&name=$2 [L,NC,QSA]

PS: Make sure your static includes like css, js, images etc have absolute path rather than a relative one.

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