Redirect htaccess part of url - .htaccess

I want to create a sort of wildcard to redirect to a new url.
I replaced a specific part, that should be redirected.
All url's that contain the following /computers/notebooks-laptops/
Should be redirect to /computers/laptops/
So the following URL
https://www.example.com/computers/notebooks-laptops/product1
should become:
https://www.example.com/computers/laptops/product1
How can I achieve that?

I am assuming you are using the .htaccess in root directory, use below rule.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/computers/notebooks-laptops/(.*)
RewriteRule ^ /computers/laptops/%1 [R=301,L]

Related

How to use .htaccess to redirect while maintaining the original URL

I am trying to use the .htacces file to redirect any files or subfolders in a derectory to one file, while still maintaining the original URL input.
So if a user goes to:
https://example.com/folder1/folder2/folder3/
or
https://example.com/folder1/folder2/file.php
it would redirect them back to:
https://example.com/folder1/index.php
but the original URL input would not change.
You can use RewriteRule . In htaccess in the document root add the following rule:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/folder1/index\.php$ [NC]
RewriteRule ^folder1/.+$ /folder1/index.php [L]
This will redirect /folder1/foo/bar to /folder1/index.php without changing the url in browser.
The RewriteCond above makes sure you don't rewrite /folder1/index.php to itself (/folder1/index.php) otherwise the rule can cause an infinite loop error.
You can just make :
RedirectMatch 301 ^https://example.com/folder1/ https://example.com/folder1/index.php
This allows you to redirect from the first url in the pattern to the
second one

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.

How to redirect URLs that do not contain some specific characters by using htaccess file?

Assume that I have some URL like these:
http://mydomaindotcom/abc/123-link.html
http://mydomaindotcom/abc/page/page-link.html
Now I want to redirect all URLs like : http://mydomaindotcom/otherstring/123-link.html to http://mydomaindotcom/abc/123-link.html
or http://mydomaindotcom/anotherone/page/page-link.html to http://mydomaindotcom/abc/page/page-link.html
It means that the first segment on URL must be [abc], if not, redirect to the [abc]-beginning one.
How can I do this?
Try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/abc/ [NC]
RewriteRule ^[^/]+/(.*)$ /abc/$1 [R=301,L,NC,QSA]

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]

.htaccess 301 redirect path and all child-paths

I want accesses to e.g. www.thisdomain.com/docs/path1/path2 to redirect to www.thatdomain.com/path1/path2
(Note that docs is not a part of the new path)
I have the following on www.thisdomain.com:
RewriteEngine on
RewriteRule ^docs/* http://www.domain.com/ [R=301,L]
If I access www.thisdomain.com/docs, it directs to www.thatdomain.com, but if I access a child-path like www.thisdomain.com/docs/path1/path2 it fails. Is it possible for the redirect to intercept the child-path access and redirect as I need? If so, any pointers?
Thanks.
With regular expressions, * means any number of the previous atom, which will match /docs and /docs/. Try this:
RewriteEngine on
RewriteRule ^docs$ http://www.domain.com/ [R=301,L,QSA]
RewriteRule ^docs/(.*) http://www.domain.com/$1 [R=301,L,QSA]
(QSA is query string append, so /docs/foo?bar=baz won't lose the ?bar=baz.)
According to section "Redirect Old domain to New domain using htaccess redirect" of the first Google result which I found searching for "htaccess redirect" (without the double quotes), this piece of code will suffice:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
According to their description of the technique, it will redirect the directory the .htaccess file is placed in recursively (including any child paths), just as you intend. Of course, mod_rewrite needs to be present for the rewrite directives to work.

Resources