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

谁都会走 提交于 2019-12-24 16:39:35

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/9262720/htaccess-pretty-url-rewrite-for-php-pages-in-a-subfolder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!