【问题标题】:Mod Rewrite: How to rewrite URL passing everything to index.phpMod Rewrite:如何重写将所有内容传递给 index.php 的 URL
【发布时间】:2017-03-24 10:21:13
【问题描述】:

我启用了 mod_rewrite 并使用phpinfo() 进行了检查。

我放置了一个 .htaccess 文件

RewriteEngine on

RewriteRule ^(.*)$ index.php?/$1 [L]

和 apache 根目录 (/var/www/html/) 中的 index.php 文件。

工作网址:

http://192.168.56.101/index.php

http://192.168.56.101/index.php/edrgderg

失败的 (404) 网址:

http://192.168.56.101/sgghsshgfhfgj

Mod_rewrite 应该将其形成为 http://192.168.56.101/index.php/sgghsshgfhfgj,但事实并非如此。

我想将每个请求重定向到index.php

解决方案:

我不得不编辑我的 apache 配置:

sudo nano /etc/apache2/sites-enabled/000-default.conf

我补充说:

<Directory /var/www/html>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
</Directory>

这里建议:https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite-for-apache-on-ubuntu-14-04

【问题讨论】:

  • 你想达到什么目的?看起来您想将每个请求重定向到 index.php
  • 没错,我的实际重写更具体一些,但这是(不工作,)棘手的部分。

标签: php apache .htaccess mod-rewrite url-rewriting


【解决方案1】:

从重写规则中排除现有文件/目录以避免循环规则:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?/$0 [L,QSA]

【讨论】:

  • 我试过你的版本和一个类似的版本:RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php/$1 [L] 两者都不适合我。
  • 没有其他规则。我确实输入了这个 URL,但我希望将 http://192.168.56.101/sgghsshgfhfgj 传递给 index.php
  • 不,仍然得到 404。
  • ok 然后验证您的.htaccess 是否已启用,方法是在您的.htaccess 顶部放置一些垃圾(随机)文本并查看它是否在何时生成500(内部服务器)错误您在浏览器中访问您的页面。 500 错误意味着 .htaccess 已启用。
  • 谢谢,mod_rewirte 没有正确启用。我更新了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多