Apache 2中的URL缩短

问题描述

| 我的网站首页为as0ѭ 现在我想将
www.mysite.com/x2312d
指向
/var/www/mysite/app/pass.PHP?id=x2312d
我该怎么做呢? 我应该在哪里创建
.htaccess
文件,该文件内容是什么?     

解决方法

在你的根
/var/www
.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\\.]+)$ /app/pass.php?id=$1 [NC,L]
    ,将.htaccess文件放在/ var / www / mysite / app /中。我假设您的docroot是/ var / www / mysite / 确保apache 2已启用重写模块 .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]
我喜欢这种方法,假设pass是某种控制器。我的rest api控制器从$ _SERVER全局变量手动解析/ x23123d,但是您可以使用其他重写器将其放在$ _GET / $ _ REQUEST [\'id \']全局变量中。 像这样的东西:
RewriteRule ^app/(.+)$ app/pass.php?id=$1 [PT,L,QSA]
良好参考:http://httpd.apache.org/docs/2.0/misc/rewriteguide.html http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule 编辑:几乎忘记了,不要忘记也处理斜杠。