使用mod_rewrite更改Apache中的站点URL

时间:2009-09-17 20:26:58

标签: apache .htaccess mod-rewrite

如何使用mod_rewrite更改我的网站网址我的问题是当我打开网站的管理部分时,我必须写www.example.com/index.php/admin

我想要的是直接打开我的网站,例如www.example.com/admin请帮助

感谢

2 个答案:

答案 0 :(得分:1)

这是从CodeIgniter PHP框架中复制的.htaccess文件:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

这会将index.phprobots.txtimages文件夹中的所有内容重定向到index.php/$1,其中$1是用户输入的网址

您需要以下规则:

  • 重定向index.php会导致infinte循环 - http://localhost/index.php/index.php/index.php/...
  • robots.txt是搜索引擎使用的重要文件。这只是纯文本;你不希望它由你的代码处理。
  • 您显然希望继续访问您的图片。根据您的静态资产所在位置调整此路径。

答案 1 :(得分:1)

尝试此规则:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^index\.php/ index.php%{REQUEST_URI} [L]

条件是排除要重写的现有文件的请求。