URL Shortening in Apache 2
I have a site开发者_JS百科 with homepage as /var/www/mysite/index.html
Now I want to direct www.mysite.com/x2312d
to /var/www/mysite/app/pass.php?id=x2312d
How do I do this?
Where should I create .htaccess
file and what should be the contents of it?
in your root /var/www
.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ /app/pass.php?id=$1 [NC,L]
put the .htaccess file in /var/www/mysite/app/. I'm assuming your docroot is /var/www/mysite/
make sure apache 2 has the rewrite module enabled
.htaccess:
RewriteEngine On
#allows you to still access static items in this dir
RewriteCond %{REQUEST_URI} !\.(php|css|js|gif|png|jpe?g)$
#sent it all to pass script
RewriteRule (.*)$ pass.php [L]
I like this method, assuming pass is some sort of controller. My controller for a rest api parses the /x23123d manually from the $_SERVER global var, but you could use a different rewriterule to have it be in the $_GET/$_REQUEST['id'] global var.
something like this:
RewriteRule ^app/(.+)$ app/pass.php?id=$1 [PT,L,QSA]
Good References: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule
edit: almost forgot, don't forget to handle trailing slashes too.
精彩评论