简体   繁体   English

PHP SEO 友好,带有干净的 URL

[英]PHP SEO friendly with clean URL

Currently I works on to transfer my site into SEO friendly URL (in localhost),目前我致力于将我的网站转移到 SEO 友好的 URL(在本地主机中),

Here's the original URL with query string:这是带有查询字符串的原始 URL:

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

I want convert to:我想转换为:

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

.htaccess file (same directory with item.php): .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>

in item.php , I use $_GET['category']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();
}

Problem 1: When I loaded http://{ip}/sitename/item/category/44 , the page cannot get the variable passes to get_item() , the page is shown Page not found ?问题 1:当我加载http://{ip}/sitename/item/category/44 ,页面无法获取传递给get_item()的变量,页面显示Page not found 44 is suppose return a value. 44 是假设返回一个值。

Problem 2: My page doesn't loaded referrer files, all *.css *.js etc are just ignore?问题 2:我的页面没有加载引用文件,所有*.css *.js等都被忽略了?

Try this in your .htaccess file:在你的 .htaccess 文件中试试这个:

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

Problem 1: No rewriting has happened here – (you only think it has, beause your script item.php got called anyway, because you had item in the URL and MultiViews has done its work) – paths the RewriteRules match on in .htaccess never start with a / , so remove it.问题 1:这里没有发生重写——(你只是认为它发生了,因为你的脚本item.php无论如何item.php被调用了,因为你在 URL 中有item并且 MultiViews 已经完成了它的工作)——RewriteRules 在 .htaccess 中匹配的路径永远不会/开头,因此将其删除。

Problem 2: Has been discussed many times before (and should also be obvious to anyone who knows the basics of how completion of relative URLs works) – see fe .htaccess URL Rewrite Problem (Scripts don't load)问题 2:之前已经讨论过很多次了(对于了解相对 URL 补全工作原理的人来说也应该是显而易见的)——参见 fe .htaccess URL Rewrite Problem (Scripts don't load)

PROBLEM 2 - The problem has arised as you have not mentioned the base path.问题 2 - 由于您没有提到基本路径,因此出现了问题。

You need to assign base path to load css and other references.您需要分配基本路径来加载 css 和其他引用。

Try using this in <head> tag <base href="http://www.MYSITE.com/" />尝试在<head>标签<base href="http://www.MYSITE.com/" />

This will load your css/js.这将加载您的 css/js。

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

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

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