简体   繁体   中英

Create SEO-friendly URL from php using htaccess

I have a problem where my site url is as below;

myhomesite.ext/listino/macchine/listino/marca.php?marca=Audi

and I want to obtain an url similar as follows

myhomesite.ext/listino/macchine/listino/marca-Audi/

So, I try with the following code

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
</IfModule>


<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^marca/([a-zA-Z0-9]+)/$ marca.php?marca=$1
</IfModule>

but nothing happened, I don't understand and please point me out where is my mistake.

This is my latest add from the comment

Oh yes it's correct i am trying several solutions, but now i 've tried this code from one generator tool it's

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

if i go on mediasportweb.com/macchine/listino/marca/marca/15 i find the url, with many problem on page on the css style, but i find it, but i also find the url mediasportweb.com/macchine/listino/marca.php?marca=15, i think that directive rewrite url erase the old url, what is wrong in my think?

Note that the url you shoud obtain from your code is:

myhomesite.ext/macchine/listino/marca/Audi/

and not:

myhomesite.ext/macchine/listino/marca-Audi/

How about changing to

RewriteRule ^marca\-([a-zA-Z0-9]+)/$ marca.php?marca=$1

from

RewriteRule ^marca/([a-zA-Z0-9]+)/$ marca.php?marca=$1

Place this rule in /macchine/listino/.htaccess :

RewriteEngine On
RewriteBase /macchine/listino/

RewriteCond /marca\.php\?marca=([^\s&]+) [NC]
RewriteRule ^ marca/marca/%1? [R=302,L]

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

For css/js etc use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash / .

You can try adding this in your page's HTML header: <base href="/" />

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