【问题标题】:PHP SEO friendly with clean URLPHP SEO 对干净的 URL 友好
【发布时间】:2014-05-19 09:21:31
【问题描述】:

目前我正在努力将我的网站转换为 SEO 友好的 URL(在本地主机中),

这是带有查询字符串的原始 URL:

http://{ip}/sitename/item.php?category=44

我想转换为:

http://{ip}/sitename/item/category/44

.htaccess 文件(与 item.php 相同的目录):

DirectoryIndex index.php
Options -Indexes

<files page>
ForceType application/x-httpd-php
</files>

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^.*$ item.php%{REQUEST_URI} [L]
    RewriteRule ^/item/([0-9]+) /item.php?category=$1
</IfModule>

<Files *htaccess>
Deny from all
</Files>

item.php,我使用$_GET['category']

$category = preg_replace('/[^0-9]/', '', $mysqli->real_escape_string($_GET['category']));

$list = $listing->get_items($category, $mysqli);

if($list == 0){
echo '<p><h2>Page not found</h2></p>';
die();
}

问题 1: 当我加载http://{ip}/sitename/item/category/44时,页面无法获取变量传递给get_item(),页面显示Page not found? 44 是假设返回一个值。

问题 2: 我的页面没有加载referrer文件,所有*.css*.js等都被忽略了?

【问题讨论】:

  • 你的重写基定义在哪里?我确定您错过了正确的位置。

标签: php .htaccess friendly-url


【解决方案1】:

重写引擎开启
RewriteBase /站点名称
RewriteRule ^item/([0-9]+)/?$ item.php?category=$1 [L,NC]

【讨论】:

    【解决方案2】:

    在你的 .htaccess 文件中试试这个:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule item/category/(.*) item.php?category=$1
    

    【讨论】:

      【解决方案3】:

      问题 2 - 由于您没有提到基本路径,因此出现了问题。

      您需要指定基本路径以加载 css 和其他引用。

      尝试在&lt;head&gt;标签&lt;base href="http://www.MYSITE.com/" /&gt;中使用它

      这将加载你的 css/js。

      【讨论】:

      • 我使用@Neeraj Kumar 脚本并添加了&lt;base href="http://www.MYSITE.com/" /&gt;,现在页面运行良好。谢谢!!
      【解决方案4】:

      问题 1:这里没有发生重写——(你只是认为它有,因为你的脚本 item.php 无论如何都被调用了,因为你在 URL 中有 item 并且 MultiViews 已经完成了它的工作)—— RewriteRules 匹配的路径on in .htaccess 从不/开头,所以删除它。

      问题 2:之前已经讨论过很多次(对于知道如何完成相对 URL 工作原理的任何人来说,这也应该是显而易见的)——参见 f.e. .htaccess URL Rewrite Problem (Scripts don't load)

      【讨论】:

        猜你喜欢
        • 2013-03-15
        • 1970-01-01
        • 2016-04-13
        • 2011-05-09
        • 2018-05-22
        • 1970-01-01
        • 2012-11-09
        • 2015-01-13
        • 2012-08-21
        相关资源
        最近更新 更多