Apache mod-rewrite for shorter urls
Is it possible do do something like this开发者_开发问答 with mod-rewrite?
Current url:
www.example.com/Departments/dynamicPage.php?DeptID=10&DeptName=HR
to set up a rewrite so:
www.example.com/hr
could redirect to the above (with the arguments)?
I know I could create an "hr" folder on the root level and put in an html page with a meta refresh, but I hate the extra clutter.
I don't think a .htaccess 301 is possible, but please correct me if I'm wrong. I'm looking for an elegant solution that can be added to for future instances.
Seems reasonable. Something along the lines of this should do the trick:
RewriteRule ^hr$ /Departments/dynamicPage.php?DeptID=10&DeptName=HR [L]
If you want to make it generic:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /Departments/dynamicPage.php?DeptID=10&DeptName=$1 [L]
Of course, then you need to be careful about people heading to departments which don't actually exist, and you'll need to make sure all your DeptNames make sense.
If you want a 301 redirect, use [R=301]
or [L,R=301]
at the end of the rewrite rule.
RewriteEngine On
RewriteRule ^hr$ Departments/dynamicPage.php?DeptID=10&DeptName=HR [R=301]
精彩评论