【问题标题】:Can't write the right htaccess file for a url redirect无法为 url 重定向编写正确的 htaccess 文件
【发布时间】:2018-10-25 16:52:37
【问题描述】:

我已经阅读了大量关于此案的其他答案,但没有任何帮助。我有这个网站http://allutas.com 具有以下文件夹结构

Allutas
 Application 
   Controllers
      index.php
   Models
   Views
 System
 index.php

当您访问http://allutas.com 时,索引页面会执行一些检查,如果一切正常,它会将用户发送到 Application/Controllers/index.php

用户可以访问的所有其他页面也位于该 Controllers 文件夹中

所以我想要做的就是从控制器文件夹中的所有文件以及 .php 扩展名中删除或隐藏我网站中的子目录部分,而不是

http://allutas.com/Application/Controllers/index.php 要么 http://allutas.com/Application/Controllers/about.php

我希望它是那样的

http://allutas.com/index 要么 http://allutas.com/about

我尝试了很多其他答案,但他们最终什么也没做或导致 500 内部服务器错误

这是一个 htaccess 文件的示例,它会给出 500 内部服务器错误

 <IfModule mod_rewrite.c>
 RewriteEngine On    
 RewriteRule ^(.*)$  application/controllers/$1 [L]
 </IfModule>

如果有帮助,我的主机使用的是 Apache 版本 2.4.3-25 和 PHP 版本 5.6.27

【问题讨论】:

  • application != Application 检查您的情况。
  • 虽然您真的想研究使用路由器来加载控制器。因为您将更好地控制无效路线并能够相应地处理它们。
  • @LawrenceCherone 我尝试了大写和小写都没有变化

标签: php apache .htaccess url-rewriting http-redirect


【解决方案1】:

你能试试这个吗-

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /Allutas/
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ Application/Controllers/$1 [L]    
</IfModule>

它应该适用于这些类型的链接: http://allutas.com/about.php http://allutas.com/index.php

【讨论】:

  • 它会导致 500 Internal Server Error 知道吗?
【解决方案2】:

在 .htaccess 文件中:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule  ^(.+)$ index.php?url=$1 [QSA,L]

进入索引。将$_GET['url'] 设置为站点域名之后的任何内容。

在您的 index.php 文件中(在 .htaccess 文件旁边):您应该能够获得 @ 的内容987654323@轻松无忧!

$url = $_GET['url'];
echo $url;

你会得到:

文本1/文本2/文本3

通常这样使用,

  1. text1 指的是控制器
  2. text2 指的是一个动作
  3. text3 引用一个参数

随心所欲!

【讨论】:

    猜你喜欢
    • 2020-01-15
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多