5

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?

  • I assume that Category.php is still the actual file that is handling the category pages? If so, where is your rewrite rule to it? – Stephen Ostermiller Jun 19 '17 at 18:03
  • can you give me full htaccess rules to do this? – Mohammadreza Jun 19 '17 at 18:24
  • So which do you want? Do you want the rules to redirect away from the parameters, or do you want the rules to implement the friendly URLs? – Stephen Ostermiller Jun 19 '17 at 18:40
  • Just to confirm... 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? – MrWhite Jun 19 '17 at 22:11
  • 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...thanks – Mohammadreza Jun 20 '17 at 3:28
4

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.

  • 1
    so helpfull...another thing..sorry for my english..i can't speak well...how ever thanks god you guys are here :) – Mohammadreza Jun 20 '17 at 17:46
  • 1
    Thanks. I had to add RewriteCond %{ENV:REDIRECT_STATUS} ^$ to prevent both rules being somehow strangely mixed together (like if L was not being considered). (+1) – Martin Prikryl Apr 24 at 14:05
  • @MartinPrikryl Yes, thanks, missed that! That condition is required to prevent the rewritten URL being redirected and causing a redirect loop. We only want to redirect the initial request. I've updated my answer and added a bit of explanation in that regard. (Also noticed a typo in the query string that had unfortunately propagated through copy/paste - fixed that.) – MrWhite Apr 24 at 20:04
-1

I think something like this should work.

RewriteEngine On
RewriteCond %{HTTP} off
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^Category.php?Category_Id=/(.*)$ /category/$1 [R=301,NC,L]
RewriteRule ^&Category_Title=/(.*)$ /$1 [R=301,NC,L]
  • i changed generated link from bad seo structure to seo friendly structure and it works – Mohammadreza Jun 20 '17 at 11:46
  • 1
    Sorry, I'm going to have to downvote - there's no way this answer would get even close to "working" (regardless of the question). The first rule doesn't appear to have anything to do with the question (it looks like a malformed HTTPS/HTTP redirect - but why?). The next two rules won't do anything either: you can't match the query string with the RewriteRule pattern. And the substitution strings are also incorrect. But this is also (somehow) split across two directives?! Etc. etc. – MrWhite Jun 20 '17 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.