简体   繁体   English

htaccess 如何重写这个 url

[英]htaccess how to rewrite this url

How to rewrite this URL:如何重写这个 URL:

https://example.com/illustrations.php?category=cats&cat_id=1

to:至:

https://example.com/category/cats

also, how do I still preserve cat_id param?另外,我如何仍然保留cat_id参数?

I tried this but it does not work:我试过这个但它不起作用:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/(.*?)/?$ /illustrations.php?category=$1 [L]

When I add this code and go to illustrations.php?category=cats it does not change the URL in the browserbar.当我将此代码和 go 添加到illustrations.php?category=cats它不会更改浏览器栏中的 URL。

Yes, that is "correct".对,那是正确的”。

The code you posted internally rewrites the URL /category/cats (which is the URL you should be linking to in your HTML source) back to the actual filesystem/URL path: /illustrations.php?category=cats . The code you posted internally rewrites the URL /category/cats (which is the URL you should be linking to in your HTML source) back to the actual filesystem/URL path: /illustrations.php?category=cats . This is required in order to make the "pretty" URL /category/cats "work".这是使“漂亮的” URL /category/cats “工作”所必需的。

You can't change the URL structure using .htaccess only - if that is what you are implying.您不能仅使用.htaccess更改 URL 结构 - 如果这是您所暗示的。 You do need to actually change the physical URLs in your HTML source.您确实需要实际更改 HTML 源中的物理 URL。

You could implement an external redirect (not a rewrite) from /illustrations.php?category=cats to the canonical URL /category/cats using .htaccess , but note that this is only to benefit SEO (and third parties that might have already linked to the old URLs). You could implement an external redirect (not a rewrite) from /illustrations.php?category=cats to the canonical URL /category/cats using .htaccess , but note that this is only to benefit SEO (and third parties that might have already linked to旧网址)。 This is a necessary additional step if you are changing an existing URL structure and SEO is a concern, but it is not part of the "working" of your site.如果您要更改现有的 URL 结构并且 SEO 是一个问题,这是一个必要的附加步骤,但这不是您网站“工作”的一部分。

how do I still preserve cat_id param我如何仍然保留cat_id参数

You would need to include the value of the cat_id parameter in the URL.您需要在 URL 中包含cat_id参数的 eg.例如。 /category/cats/1 (as @arkascha suggested in comments) or /category/1/cats - depending on which value is more important. /category/cats/1 (正如@arkascha 在评论中建议的那样)或/category/1/cats - 取决于哪个值更重要。 I would put the more important value first, since URLs can be accidentally cropped when shared.我会把更重要的价值放在第一位,因为 URL 在共享时可能会被意外裁剪。

 RewriteEngine on RewriteCond %{REQUEST_FILENAME}?-f RewriteCond %{REQUEST_FILENAME}.-d RewriteRule ^/?category/(?*.)/?$ /illustrations.php?category=$1 [L]

This rule could be simplified.这条规则可以简化。 Filesystem checks are relatively expensive.文件系统检查相对昂贵。 It looks like you could remove both of these by making your regex more specific.看起来你可以通过使你的正则表达式更具体来删除这两个。 eg.例如。 Could /category/1/cats ever map to a file? /category/1/cats可以将 map 保存到文件中吗? Do you need directories to be accessible?您需要可访问目录吗? I would expect the answer to both those questions is "no".我希望这两个问题的答案都是“不”。

I would also decide on whether to allow trailing slashes or not, rather than allowing both (as in your current rule).我还将决定是否允许尾部斜杠,而不是允许两者(如您当前的规则)。 Strictly speaking this creates duplicate content (two URLs; same content), so requires additional steps to resolve.严格来说,这会创建重复的内容(两个 URL;相同的内容),因此需要额外的步骤来解决。 Your example URL(s) do not contain a trailing slash.您的示例 URL 不包含尾部斜杠。

So, you could simplify your rules to the following in order to rewrite /category/1/cats (no trailing slash) to /illustrations.php?category=cats&cat_id=1因此,您可以将规则简化为以下内容,以便将/category/1/cats (无尾随斜杠)重写为/illustrations.php?category=cats&cat_id=1

RewriteEngine On
RewriteRule ^category/(\d+)/([\w-]+)$ /illustrations.php?category=$2&cat_id=$1 [L]

This assumes cat_id can be 1 or more digits ( 0-9 ).这假设cat_id可以是 1 个或多个数字( 0-9 )。 And category is limited to the following characters: az , AZ , 0-9 , _ (underscore), - (hyphen).并且category仅限于以下字符: azAZ0-9_ (下划线)、 - (连字符)。

With regex it is preferable to be as restrictive as possible.对于正则表达式,最好尽可能限制。 By omitting the dot ( . ) from the last path segment in the above rule it cannot match a physical file (assuming all your files have file extensions).通过在上述规则的最后一个路径段中省略点 ( . ),它不能匹配物理文件(假设所有文件都有文件扩展名)。

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

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