繁体   English   中英

.htaccess加载index.php文件以获取友好的URL

[英].htaccess to load index.php file for friendly url

我有这个网址:

www.mywebsite.com/news/best-ever-phones

best-ever-phones是可变的,可以是任何东西。
index.php文件正在使用url best-ever-phones检查数据库中的文章

问题是在./news/.htaccess文件中我有以下代码:

RewriteEngine On
RewriteBase /news/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?article=$1 [L,QSA]

当我访问www.mywebsite.com/news/best-ever-phones网址时,应该加载index.php文件,但服务器返回错误404 Not found

怎么了?

尝试这个

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule   ^(.+)$   index.php?article=$1   [L]

我认为问题可能出在重写基础和您的相对路径上。 但这就是我要做的。

<IfModule mod_rewrite.c>

   Options -Indexes

   RewriteEngine On
   RewriteRule ^news/([A-Za-z-.]+)/?$ news.php?article=$1  [NC]

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

   RewriteRule ^([^\.]+)$ $1.php [NC,L]
  </IfModule>

这应该可以解决问题,将索引重命名为news.php

也许试一下:

RewriteEngine On
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-\/]+)\/?$ index.php?article=$1 [L,QSA]

这是假定您希望/字符出现在$article变量中,因此,如果要在/news/index.php ,可以在逻辑上分隔友好URI的不同部分。 否则,单段友好的URI(例如/news-item-1或诸如此类)可以以斜杠结尾或不以斜杠结尾。

确实,您不希望使用斜杠,因为使用相同的URL加载同一页面可能会导致重复的内容罚款(/计数),因此您可能无法匹配\\/? 捕获组外部或301中的index.php重定向到URL,并删除了斜杠。 这样,您还可以获得干净的查询参数,例如/news/something-big?orly=yes而不是/news/something-big/?orly=yes

我正在使用此站点来测试重写。 否则未经测试,因为我们没有您的代码,因此无法判断它是否在您的环境中有效。

当我们单击任何URL时,将使用该代码隐藏其扩展名。 但是,如果要转到index.php页面,请编写

echo '<a href="index.php">Index Page </a>';

  ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM