htaccess rewrite replace part of url - .htaccess

I'd like to rewrite part of URLS like:
http://www.myweb.com/cat1/subcat2/how-to-do-this
to
http://www.myweb.com/cat2/subcat2/how-to-do-this
there are many urls in which the end part(how-to-do-this) changes
I have tried this:
RewriteRule ^cat1/subcat1/(.*) /cat/subcat2 [R=301,L,NC]
but this doesn't works.
Please help.

R=301 is used for external redirection, if you want to rewrite the request , you can use the following :
RewriteRule ^cat1/subcat2/(.*) /cat2/subcat2/$1 [L,NC]
This will internally redirect (without changing the url in browser)
cat1/subcat2/foo
to
cat2/subcat2/foo

Related

Redirect URL using rewrite rules- https://hostname/en/<content-page> to https://hostname/en/<storename>/content-page>

Redirect dynamic URL for below example :
https://hostname/en/content-page
to
https://hostname/en/storename/content-page
content-page is the dynamic parameters Like-
https://hostname/en/grocery/vegetables?fetchFacets=true#facet:&productBeginIndex:0&facetLimit:&orderBy:&pageView:grid&minPrice:&maxPrice:&pageSize:&
to
https://hostname/en/storename/grocery/vegetables?fetchFacets=true#facet:&productBeginIndex:0&facetLimit:&orderBy:&pageView:grid&minPrice:&maxPrice:&pageSize:&
OR
http://hostname/en/electronics?fetchFacets=true#facet:&productBeginIndex:0&facetLimit:&orderBy:&pageView:grid&minPrice:&maxPrice:&pageSize:&
to
http://hostname/en/storename/electronics?fetchFacets=true#facet:&productBeginIndex:0&facetLimit:&orderBy:&pageView:grid&minPrice:&maxPrice:&pageSize:&
add /storename or /en/storename in dynamic URLs
You should be able to just put /en/ in your pattern and then substitute it with an appended storename. Add this to your .htaccess file:
RewriteEngine On
RewriteRule ^en/(.*)$ https://hostname.com/en/storename/$1 [R=301,L]
That will redirect as you've asked in your examples.

301 redirect: redirect urls with .php to folder

In .htaccess I want to redirect urls like this:
/products.php/a7-frames
/products.php/a6-frames
to this:
/picture-frames/a7-frames
/picture-frames/a6-frames
So need to substitute products.php with picture-frames.
After a lot of Googling I tried this:
RewriteBase /
RedirectMatch 301 (.*)\.php/?$ https://www.domainname.com/picture-frames$1
But it doesnt work, if I enter this url: /products.php/a7-frames the browser says there are too many redirects and goes to:
/picture-frames/index
It's substituting the products.php for picture-frames which is great, but I'm not sure why it adds "index" on the end rather than the /a7-frames part of the url? How can I fix this?
You can use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+products\.php/([^\s?]+) [NC]
RewriteRule ^ /picture-frames/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^picture-frames/([^/]+)/?$ products.php/$1 [L,NC]
Make sure to clear your browser cache before testing this change.

rewrite rule to convert php querystring to html

Please help me in writing correct .htaccess code to get the wanted result.
I have the following pages:
www.mydomain.com/?exam=/hp-certification/
www.mydomain.com/?exam=/cisco-certification/
www.mydomain.com/?exam=/oracle-exam-prep/
www.mydomain.com/?exam=/faq/
www.mydomain.com/?exam=/support/
And there are many more pages but you can notice that the fixed part is: www.mydomain.com/?exam=
Instead of using query string links, I want the following links to work as above pages:
www.mydomain.com/hp-certification/
www.mydomain.com/cisco-certification/
www.mydomain.com/oracle-exam-prep/
www.mydomain.com/faq/
www.mydomain.com/support/
and so on.
your link should be like this www.mydomain.com/?exam=hp-certification/ and the rewrite rule would like this in .htaccess file.
RewriteEngine On
RewriteRule ^([^/.]+)$ /?exam=$1 [L]
You can do that by using the following rules in your .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} exam=(.+) [NC]
RewriteRule ^(.*)$ http://www.example.com/%1? [R=301,NC,L]
So what does the above do?
First, it will take the query exam= as a condition, if this condition is met then it will grab any version of the variable using (.+).
It will then rewrite the URL using 301 redirection to show http://www.example.com/anything/. The use of %1 is to grab the variable from exam= and then we use ? to stop the original query string from appearing on the end of the newly rewritten URL.
Make sure you clear your cache before testing this.

Redirect all urls which contain certain parameters to another url which follows a certain pattern

Unfortunately I didn't get it solved by myself and need to ask for help. I want to redirect all urls which follow a certain pattern (in this case it contains "reviews/category"). These URLs supposed to be redirect to another url which is made up the first one:
http://[product-url]/reviews/category/[category-url]
supposed to be redirect to
http://[product-url].html
Furthermore it shouldn't matter if you call the url with or without www.
Example:
http://example.com/ford-blues/reviews/category/cars supposed to be redirect to http://example.com/ford-blues.html
Any help would be much appreciated.
Following code isn't working
RewriteEngine On
RewriteRule ^reviews/category/?$ $1\.html [R=301,L]
Try:
RedirectMatch 301 ^(.*)/reviews/category/ /$1.html
in the htaccess file in your document root.
Or using mod_rewrite:
RewriteEngine On
RewriteRule ^(.*)/reviews/category/ /$1.html [L,R=301]

htacess url rewrites for .php?=n

I'm looking to rewrite old PHP pages to new locations, however the previous system used URLs structured as follows:
/old-page.php?page=Our%20Services
I've tried the following:
Redirect 301 /old-page.php?page=Our%20Services http://www.newdomain.co.uk/our-services/
Can someone help explain to me why the rewrite rule ignores everything after the question mark please?
You can use Redirect for simple cases only. If you want to check the query string, you must use mod_rewrite and RewriteCond
RewriteEngine on
RewriteCond %{QUERY_STRING} page=Our%20Services
RewriteRule ^old-page.php$ http://www.newdomain.co.uk/our-services/ [R,L]
This checks, if the query string contains page=Our%20Services and the URL path is old-page.php. It then redirects the client to the new URL http://www.newdomain.co.uk/our-services/.

Resources