MindSculpt
MindSculpt

Reputation: 253

.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?

Upvotes: 1

Views: 1915

Answers (1)

anubhava
anubhava

Reputation: 785246

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.

Upvotes: 3

Related Questions