【问题标题】:SEO friendly URL htaccess Mod RewriteSEO 友好的 URL htaccess Mod 重写
【发布时间】:2026-01-06 05:00:02
【问题描述】:

我目前在我的 htaccess 中有以下内容,无需用户在每个页面后都有 '.php' - 例如转到 URL.com/about 与 URL.com/about.php 的工作方式相同:

Options -Indexes     
RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} !-d     
RewriteCond %{REQUEST_FILENAME}\.php -f     
RewriteRule ^(.*)$ $1.php

我还有一个交易页面,其中 URL 中有两个变量: URL.com/deals?make=samsung&model=X500D

我想要完成的是通过以下方式访问上述网址: URL.com/samsung-X500D/(尾随窗扇是可选的)

我已尝试将以下内容添加到顶部的现有 htaccess 代码中,但我收到 500 服务器错误代码:

RewriteRule ^([^-]*)-([^-]*)$ /deals?make=$1&model=$2 [L]

我也不需要重写页面上的任何 css / img 文件 - 这只是我想要更改的 URL。

【问题讨论】:

标签: .htaccess mod-rewrite url-rewriting cpanel


【解决方案1】:

您必须确保在包罗万象的 PHP 规则之前使用特定的重写:

RewriteEngine On

RewriteRule ^([^-]+)-([^-/]+)/?$ /deals?make=$1&model=$2 [L,QSA]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

【讨论】:

  • 在你的RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC],我看不懂$1
  • $1 是从 RewriteRule 捕获的值,因为处理在 mod_rewrite 中向后发生。你测试了吗?
  • 我刚刚阅读了 RewriteCond 的文档,对于 $ 和 % 谢谢!
【解决方案2】:

尝试:

RewriteRule ^(.+)-(.+)$ deals.php?make=$1&model=$2 [L]

【讨论】: