Mass redirect urls through .htaccess - .htaccess

I changed the path of some urls and would like to get some help if possible, to create a rule in my .htaccess to redirect them in bulk.
The old url were like this one: https://www.mastersol.gr/hliakoi-thermosifones-solar/epilektikoi/mastersol-plus-wifi/product/430-160-2-plus-wifi-mastersol
The new urls are like this one: https://www.mastersol.gr/iliakoi-thermosifones/epilektikoi/mastersol-plus-wifi/product/430-160-2-plus-wifi-mastersol
As you can see, I changed only the part /hliakoi-thermosifones-solar/ to /iliakoi-thermosifones/ and I need to 301 redirect all /hliakoi-thermosifones-solar/ urls to /iliakoi-thermosifones/.
Could you help on this?
Thanks!

The task appears to be straight forward: you implement a rule matching only those requests still using the old pattern, capture the stuff following after that token to be changed and redirect (or itnernally rewrite) to a newly assembled URL:
For an external redirection:
RewriteEngine on
RewriteRule ^/?hliakoi-thermosifones-solar(.*)/?$ https://www.mastersol.gr/hliakoi-thermosifones$1 [R=301,END]
For an internal rewrite:
RewriteEngine on
RewriteRule ^/?hliakoi-thermosifones-solar(.*)/?$ /hliakoi-thermosifones$1 [END]
If you receive back an http status 500 (internal server error) using those rules then chances are that you are using a very old version of the apache http server and have to use the [L] flag instead of the [END] flag.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug, they really slow down the http server, often for nothing and they can open pathways to security nightmares. They are only provided for situations where you do not have access to the real http servers host configuration (read: really cheap service providers).

Related

Htaccess Redirect 301 from subsite.old-domain.com to new-domain.com

I have a subsite.old-domain.com (it is a WordPress Multisite installation).
I've migrated all the subsite's content to the new new-domain.com
I want to implement a Redirect 301 into my .htaccess, from old subsite to the new stand alone website.
Something like:
RewriteEngine on
RewriteRule <ALL URLS OF subsite.old-domain.com> https://www.new-domain.com/$1 [R=301,L]
Of course, other subsites inside old-site.com (for example: subsite-X.old-domain.com, subsite-Y.old-domain.com, etc) should not be influenced.
How to do it?
Sounds pretty straight forward:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subsite\.old-domain\.com$
RewriteRule ^ https://www.new-domain.com%{REQUEST_URI} [QSA,R=301,END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Rewrite with haccess an old url with unknown subfolders to a an url with only the first "subfolder" from old url

I'm trying to do a weird redirect in htaccess but i don't know how to do.
I have old urls like:
1. www.oldsite.com/known-old-folder/unknown-subfolder-1
2. www.oldsite.com/known-old-folder/unknown-subfolder-2/unknown-second-subfolder-1
i have to redirect respectively to new urls like:
1. www.newsite.com/known-new-folder/unknown-subfolder-1
2. www.newsite.com/known-new-folder/unknown-subfolder-2
I don't want to consider "unknown-second-subfolder-1" if present.
How can i do in htaccess?
I don't know what to do in my RewriteRule to ignore the "unknown-second-subfolder-1" if present.
Thanks a lot
You may use this redirect rule in known-old-folder/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/known-old-folder/(.+) [NC]
RewriteRule ^ http://www.newsite.com/known-new-folder/%1 [L,NE,R=301]
Sounds pretty straight forward. You need to implement a redirection along the lines of the following rule inside your old site's configuration:
RewriteEngine on
RewriteRule ^/?(?:[^/]+)/([^/]+)/? https://new.example.com/new/$1 [R=301]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

.htaccess Redirect only if not accessing subdirectory

Primary Domain: example.com
Product Catalog Site: products.com
Blog: example.com/blog
What i need to do is,
example.com redirect to example.com/blog/ (So, the primary domain redirects to the blog)
example.com/blog/* No redirection
example.com/(.+) redirect to products.com/$1
I have stucked with this for several hours. Please help me to solve this.
Sounds pretty straight forward, assuming that example.com and products.com are two separate http hosts:
RewriteEngine on
RewriteRule ^/?$ /blog/ [END]
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^/?(.+)$ https://products.com/$1 [R=301]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

How to Redirect To Rewritten Url

Guys i have this following code which is does its job quite good but there is one problem when i go example.com/ara.php?name=hey it doesnt redirect to
example.com/pdf-ara/hey i need help.
but when i go to example.com/pdf-ara/hey it works
RewriteEngine On
RewriteRule ^pdf-ara/([0-9a-zA-Z-_]+)$ ara.php?name=$1 [L,QSA]
So now your question is why the internal rewriting works, but no external redirection is performed, right?
Well the answer is: you did not implement any such rule. Take a look at this appraoch:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^name=([\d\w-_]+)$
RewriteRule ^/?ara\.php$ /pdf-ara/%1 [R=301]
RewriteRule ^/?pdf-ara/([\d\w-_]+)$ /ara.php?name=$1 [END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Problem using regex to adjust query strings in htaccess redirectmatch

There's income traffic to my website with a pattern like this:
/index.php?title=xxx&somethingelse=yyy
And I need to turn it into this pattern:
/somefolder/xxx/
I've tried different things, such as:
RedirectMatch 301 ^/index.php\?title=([^&]*).*$ https://example.com/somefolder/$1/
The above code, and many other alternatives that I've tried based on online posts and articles didn't work. Maybe I'm just making a silly mistake somewhere; it's been half a day that I'm trying without success!
I appreciate your help.
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)title=([^&]*)(?:&|$)
RewriteRule ^/?index\.php$ https://example.com/somefolder/%1/ [R=301]
RewriteRule ^/?somefolder/(.+)/?$ https://example.com/index.php?title=$1 [END,QSA]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Resources