简体   繁体   中英

PHP SEO friendly with clean URL

Currently I works on to transfer my site into SEO friendly URL (in localhost),

Here's the original URL with query string:

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

I want convert to:

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

.htaccess file (same directory with 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']

$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 ? 44 is suppose return a value.

Problem 2: My page doesn't loaded referrer files, all *.css *.js etc are just ignore?

Try this in your .htaccess file:

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.

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)

PROBLEM 2 - The problem has arised as you have not mentioned the base path.

You need to assign base path to load css and other references.

Try using this in <head> tag <base href="http://www.MYSITE.com/" />

This will load your css/js.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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