Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用htaccess从php创建SEO友好的URL_Php_.htaccess - Fatal编程技术网

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

使用htaccess从php创建SEO友好的URL,php,.htaccess,Php,.htaccess,我有一个问题,我的网站网址如下 myhomesite.ext/listino/macchine/listino/marca.php?marca=Audi 我想获得如下类似的url myhomesite.ext/listino/macchine/listino/marca-Audi/ 因此,我尝试使用以下代码 <IfModule mod_rewrite.c> Options +FollowSymLinks RewriteEngine On </IfModule

我有一个问题,我的网站网址如下

myhomesite.ext/listino/macchine/listino/marca.php?marca=Audi
我想获得如下类似的url

myhomesite.ext/listino/macchine/listino/marca-Audi/
因此,我尝试使用以下代码

<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>

如果我在mediasportweb.com/macchine/listino/marca/marca/15上查找url,页面上的css样式有很多问题,但我找到了,但我也找到了url mediasportweb.com/macchine/listino/marca.php?marca=15,我认为该指令重写url会删除旧url,我认为有什么不对?

请注意,您应该从代码中获得的url是:

myhomesite.ext/macchine/listino/marca/Audi/
而不是:

myhomesite.ext/macchine/listino/marca-Audi/
换成新的怎么样

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


将此规则放入
/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]
对于css/js等,在css、js、图像文件中使用绝对路径,而不是相对路径。这意味着您必须确保这些文件的路径以
http://
或斜杠
/
开头


您可以尝试在页面的HTML标题中添加以下内容:

是的,这没关系,您有理由,但我什么也得不到……如果我编写myhomite.ext/macchine/listino/marca.php?marca=Audi,我就不会重写url,为什么?@Fabrizioatani当您键入“myhomestite.ext/macchine/listino/marca/Audi/”时,重写规则不会显示“myhomestite.ext/macchine/listino/marca.php?marca=Audi”。该规则只允许您的PHP页面读取干净url中的$\u GET参数,与读取凌乱url的方式相同。所以要获得你的页面,你应该去“myhomesite.ext/macchine/listino/marca/Audi/”好的,我同意你的观点,但我认为url搜索引擎优化的正确解决方案是重定向,而不是重写规则。它是正确的吗?什么都没有。我忘了它不是主目录。但是一个名为listinoI的sub在
look here
之后没有看到任何文本。你有没有留下不完整的评论?哦,很抱歉,这是我尝试的链接,但我也尝试过在这里使用mod rewrite生成,但什么都没有,没有解决方案…我不明白。问题说php文件链接是
/listino/macchine/listino/marca.php?marca=Audi
,但你的评论说:
/macchine/listino/marca.php?marca=5
哪一个是正确的?哦,是的,它是正确的,我正在尝试几种解决方案,但现在我已经从一个生成器工具中尝试了这段代码,它的Options+FollowSymLinks RewriteEngine on RewriteRule marca/marca/(.*)/marca.php?marca=$1 RewriteRule marca/marca/(.*)marca.php?marca=$1如果我继续查找url,页面上的css样式有很多问题,但我找到了,但我也找到了url,我认为指令重写url删除旧的url,我认为有什么不对?难以从注释中读取代码。编辑您的问题并粘贴此工作代码,以便我可以提出建议。
RewriteRule ^marca/([a-zA-Z0-9]+)/$ marca.php?marca=$1
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]