>我有一个网站,主页/var/www/mysite/index.html
现在我想引导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/中。我假设你的文档根是/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
编辑:差点忘了,别忘了处理尾随斜杠。