大网数据广告活动
网站运营与故障问答

Redirect from URL with parameters to SEO friendly URL in .htaccess

· 52CE 我爱测

I have category page with this structure example.com/Category.php?Category_Id=10&Category_Title=some-text i want to write htaccess rules to redirect from example.com/Category.php?Category_Id=10&Category_Title=some-text to this ex…

问题原文

原帖链接 · Mohammadreza Esmaeeli · CC BY-SA 3.0

I have category page with this structure

example.com/Category.php?Category_Id=10&Category_Title=some-text

i want to write htaccess rules to redirect from

example.com/Category.php?Category_Id=10&Category_Title=some-text

to this

example.com/Category/10/some-text

is there any way to force htaccess to do it. i want when i visit the

example.com/Category.php?Category_Id=10&Category_Title=some-text

the Category page automatically redirect to

example.com/Category/10/some-text

i use this htaccess code but it not redirect page to seo friendly url

Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^exmaple\.com$
RewriteRule ^(.*) http://exmaple.com/$1 [R=301,L]

how can i do it?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

tl;dr

# Redirect /Category.php?Category_Id=10&Category_Title=some-text
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^Category_Id=(\d+)&Category_Title=([\w-]+)$
RewriteRule ^Category\.php$ /Category/%1/%2? [R=301,L]

i changed generated link from bad seo structure to seo friendly structure and it works

Fair enough, glad you got it sorted, I assume you are using some kind of CMS? (This probably added something to your .htaccess I would think?)

Anyway, to address your initial question(s)...

Me: have you already changed the URL in your application to the "pretty" format? eg. The URL the user sees on the page is example.com/Category/10/some-text, not example.com/Category.php?Category_Id=10&Category_Title=some-‌text?

You: yes .but it's a lot.a have to make all url's pretty in all page and i'am doing this now.but i want to know if there is a htaccess solution i do it.so you say changing url in all pages is the only solution...

Whilst it's technically possible to implement this solely in .htaccess, without actually changing the URLs in your application, it's not recommended. It would deliver a bad user experience and would potentially be damaging to your site's SEO.

If you didn't change the URL in your application then every time a user clicked an internal link they would experience an external redirect (a second request) to the canonical URL (until the redirect was cached). That's an extra (unnecessary) request on your server. The old URL would be exposed to the user and copyable (eg. "Copy link address").

Ordinarily, the only reason to implement such a redirect is if the old URLs had already been linked to and/or search engines had already indexed the old URL structure. The redirect is then required in order to preserve SEO.

Internal rewrite (new "pretty" URL to old "ugly" URL)

In order to implement a user-friendly URL structure like this, you would need an internal rewrite that silently rewrites the request from the new "pretty" URL back to the old "ugly" (real) URL. The user only sees the new "pretty" URL. It is assumed from your question that you already had something like this (maybe not)? For example:

# Rewrite /Category/10/some-text
RewriteRule ^Category/(\d+)/([\w-]+)$ Category.php?Category_Id=$1&Category_Title=$2 [L]

$1 and $2 are backreferences to the captured groups in the RewriteRule pattern. ie. \d+ and [\w-]+ respectively.

If this was a new site then you could stop there, however...

External redirect (old "ugly" URL to new "pretty" URL) - optional

This redirect should go before the above rewrite in your .htaccess file:

# Redirect /Category.php?Category_Id=10&Category_Title=some-text
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^Category_Id=(\d+)&Category_Title=([\w-]+)$
RewriteRule ^Category\.php$ /Category/%1/%2? [R=301,L]

In order to check the query string part of the URL you must use a RewriteCond directive and check against the QUERY_STRING server variable (the RewriteRule pattern only matches against the URL-path part of the URL).

%1 and %2 are backreferences to the captured groups in the last matched CondPattern. ie. \d+ and [\w-]+ respectively. This is the reverse of the earlier rewrite.

The ? on the end of the RewriteRule substitution is required to remove the query string from the request. (Alternatively you can use the QSD flag on Apache 2.4+)

UPDATE: The condition that checks against the REDIRECT_STATUS environment variable ensures that we only redirect the initial request and not the rewritten request by the later directive (see above). The REDIRECT_STATUS env var does not exist (ie. is empty) on the initial request and is only set after the first internal rewrite/subrequest. (In a per-directory context (like .htaccess), the rewriting process essentially starts over after the last RewriteRule, and continues to do so until the URL passes through unchanged. The L flag simply causes the "current pass" through the rewriting process to end, triggering the process to effectively start again, using the now rewritten URL as input.)

Summary

Bringing this together, we have:

Options -Multiviews
RewriteEngine On

# Canonical www to non-www redirect
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) http://exmaple.com/$1 [R=301,L]

# Redirect /Category.php?Category_Id=10&Category_Title=some-text
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^Category_Id=(\d+)&Category_Title=([\w-]+)$
RewriteRule ^Category\.php$ /Category/%1/%2? [R=301,L]

# Rewrite /Category/10/some-text
RewriteRule ^Category/(\d+)/([\w-]+)$ Category.php?Category_Id=$1&Category_Title=$2 [L]

MultiViews will need to be disabled (if it's not already) for this particular example to work.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2017-06-19)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。