【问题标题】:.htaccess Rewrite to Codeigniter system in its own folder.htaccess 在自己的文件夹中重写到 Codeigniter 系统
【发布时间】:2016-08-30 12:53:34
【问题描述】:

我构建了一个没有 MVC 框架的开源系统。 是时候升级系统了,我将它重新构建到 Codeigniter MVC 框架中。

系统位于 /ci 文件夹中。 出于技术原因,它应该保留在此文件夹中。

我已经在路由文件夹中设置了正确的 .htaccess 来完成这项工作:

    RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/ci/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /ci/$1
# Change example.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ ci/index.php [L] 
RewriteRule ^/member$ /member [L]
RewriteRule ^/ad_images$ /ad_images [L]
RewriteRule ^/ad_images/130$ /ad_images/130 [L]
RewriteRule ^/ad_images/116$ /ad_images/116 [L]

我正在绕过成员文件夹,因为它仍在使用中,并且还允许系统使用不同的 ad_images 文件夹。

我的问题是这个。旧网站会有一个看起来像

的链接

www.domain.com/vehicles-for-sale

在新系统中这个链接其实是

www.domain.com/categories/1-0-0-0

我需要将旧网址永久移动到新网址。

为什么将它添加到 .htaccess 文件中不起作用,我应该如何编写它才能达到预期的效果?

RewriteRule ^/vehicles-for-sale$ /ci/categories/index/3-0-0-0 [L]

【问题讨论】:

  • htaccess 是每个目录的上下文,所以“^/”永远不会匹配,你需要匹配 ^something。不惜一切代价避免使用 .htaccess。这不是一个重写仓库!
  • @ezra-s 为什么说要不惜一切代价避免使用 .htaccess?另一种选择是永久重写?
  • 对于初学者来说,.htaccess 与“重写”无关。这是一种方法,因此非管理员用户可以在某些目录中设置指令,可以进行重写,但也可能存在与重写完全无关的其他指令,这取决于管理员在 AllowOverride 中设置的值。对于永久重写/重定向,您可以在虚拟主机上下文中完美地执行它们,从而使您免于每次命中至少检查每个 .htaccess 文件 3 次的 httpd 开销,以及在每个目录上下文中定义重定向的额外复杂性,因为您刚刚经历过。

标签: php apache .htaccess codeigniter mod-rewrite


【解决方案1】:

您应该省略第一个斜线。如果您想进行永久重定向,请使用R=301 标志。此外,您现在改写为ci/categories/index/3-0-0-0,而不是所需的categories/1-0-0-0。

这应该适合你:

RewriteRule ^vehicles-for-sale$ /categories/1-0-0-0 [L,R=301]

【讨论】:

    猜你喜欢
    • 2013-04-16
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 2012-12-12
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    相关资源
    最近更新 更多