: Redirect from URL with parameters to SEO friendly URL in .htaccess I have category page with this structure example.com/Category.php?Category_Id=10&Category_Title=some-text i want to write htaccess
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 ^(.*) exmaple.com/ [R=301,L]
how can i do it?
More posts by @Nimeshi995
3 Comments
Sorted by latest first Latest Oldest Best
his will turn example.com/section.php?id=X to example.com/section/X
I suggest storing the URI in a database then using section.php?uri=
For example:
example.com/section.php?uri=super-awesome
would turn into:
example.com/section/super-awesome
tl;dr
# Redirect /Category.php?Category_Id=10$Category_Title=some-text
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=&Category_Title= [L]
and 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 %{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+)
Summary
Bringing this together, we have:
Options -Multiviews
RewriteEngine On
# Canonical www to non-www redirect
RewriteCond %{HTTP_HOST} !^exmaple.com
RewriteRule (.*) exmaple.com/ [R=301,L]
# Redirect /Category.php?Category_Id=10$Category_Title=some-text
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=&Category_Title= [L]
I think something like this should work.
RewriteEngine On
RewriteCond %{HTTP} off
RewriteRule ^(.*)$ %{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^Category.php?Category_Id=/(.*)$ /category/ [R=301,NC,L]
RewriteRule ^&Category_Title=/(.*)$ / [R=301,NC,L]
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.