【问题标题】:Change site URL in Apache with mod_rewrite使用 mod_rewrite 在 Apache 中更改站点 URL
【发布时间】:2012-08-28 21:51:14
【问题描述】:

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

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

谢谢

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案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 是用户输入的URL。

    你需要这些规则:

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

    【讨论】:

    • 您的规则将阻止任何以index.phpimagesrobots.txt 开头开始的 URL 路径被重写。
    【解决方案2】:

    试试这个规则:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 2015-06-24
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多