简体   繁体   English

使用htaccess从php创建SEO友好的URL

[英]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 我想获得一个类似如下的URL

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? 如果我继续在mediasportweb.com/macchine/listino/marca/marca/15上找到网址,则在css样式的页面上有很多问题,但是我找到了,但是我也找到了网址mediasportweb.com/macchine/listino /marca.php?marca=15,我认为该指令重写url会删除旧的url,我认为这是什么问题?

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 : 将此规则放在/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. 对于css / js等,请在css,js,图像文件中使用绝对路径,而不要使用相对路径。 Which means you have to make sure path of these files start either with http:// or a slash / . 这意味着您必须确保这些文件的路径以http://或斜杠/开头。

You can try adding this in your page's HTML header: <base href="/" /> 您可以尝试将其添加到页面的HTML标头中: <base href="/" />

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

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