rewrite and redirect url problem in htaccess - .htaccess

I'd like to redirect
/any_path_to/oldfile.html
TO
/newfile.html
for my website. First / is for showing root directory.
I found below statement for using in .htaccess file but i am not sure if it is correct.
RewriteEngine On
RewriteRule ^oldfile.html$ /newfile.html [R=301,L]

Here is a version with a few corrections:
RewriteEngine On
RewriteRule /oldfile\.html$ /newfile.html [R=301]
The pattern is more precise than the one suggested by #DusanBajic in the comments.
The L flag is not required here, since this is an external redirection, which always terminates the rewriting process for obvious reasons. For internal rewritings you should consider using the end flag instead of the old L.
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 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).

Related

Need correct syntax for htaccess redirect

on domain.com/support/kb/, I need it to redirect to sub.domain.com/support/kb/
HOWEVER, I need any links that don't match domain.com/support/kb/ to not redirect. So images like domain.com/support/kb/images/yadda-yadda.jpg needs to not redirect.
How do I do this via .htaccess?
Sounds pretty straight forward:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^/?support/kb/?$ https://sub.example.com/support/kb/ [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 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).

URL Rewrite Using .htaccess (Domain Change Only)

I have this URL:
https://app.mydomain1.com/folder/plugins/mm/api.php?somevariables=1
What I want to achieve is that if "folder/plugins/mm/api.php" is found in the URL, rewrite that URL to use a different domain, and rest of the URL remains the same. So, the above URL should become:
https://files.mydomain2.com/folder/plugins/mm/api.php?somevariables=1
Intent is to use a different server/url for any file upload requests.
Sounds pretty straight forward, you would have found man existing solutions here on SO...
RewriteEngine on
RewriteCond %{HTTP_HOST} ^app\.mydomain1\.com$
RewriteRule ^/?folder/plugins/mm/api\.php$ https://files.mydomain2.com%{REQUEST_URI} [R=301,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...
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).

htaccess - Redirect all URLs containing /THIS/something/ANDTHAT/

My CMS is running into a loop often, because impossible URLs are generated. Those URLs have the format
www.xy.com/STRING1/something/STRING2/something
or
www.xy.com/something/STRING1/somethingother/anything/STRING2/something
I want to deny or block all URLs that contain STRING1 and STRING2 as segments.
Thanks for any idea!
There are many issues with the accepted answer above, so I will post a correction...
RewriteEngine on
RewriteRule ^/?STRING1/.*/STRING2/ https://example.com/ [R=301]
Note: the L flag does not make any sense when you implement an external redirection. Also the matching is not really precise in the accepted answer...
In case you want to return an explicit http status (like 403) you can also do that:
RewriteEngine on
RewriteRule ^/?STRING1/.*/STRING2/ - [R=403]
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 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).
You can do it with this rewriting:
RewriteRule STRING1\/.*\/STRING2.* https://www.xy.com/ [R=301,L]

Redirect only parent page with subpages

I want to redirect one of the main pages of mysite with its sub pages.
My current URL;
www.mysite.com/article/service-1
www.mysite.com/article/service-2
I want to redirect to;
www.mysite.com/publication/service-1
www.mysite.com/publication/service-2
How can I redirect so?
Your question is a big vague as to what you actually try to achieve. So I will give you two answers as a starting point:
I assume you actually want an internal rewrite, not a rediretion. If so this will do:
RewriteEngine on
RewriteRule ^/?article/(.*)$ /publication/$1 [END,QSA]
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.
If you really want a redirection as you write in your question, then this variant will do, it will actually redirect the client, so change the visible URL in the browser:
RewriteEngine on
RewriteRule ^/?article/(.*)$ /publication/$1 [R=301,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...
Both rules 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).

Rewrite rule not working - Redirect to new url

we moved to the new cms, there are links that need to be redirected from the old url to the new ones, wrote the revrite rule, but nothing works, in what
Old url - site.com/1222_titlepost.html
New url - site.com/blog/{slug} - id
RewriteRule ^post/([0-9]{1,})_([0-9a-z]{1,}).html blog/$1 [NC,R=301,L]
Centos 7 , apache2 , php7 , whm+cpanel
The example you give in the comment to the question does not match what you claimed in the question. If that really expresses the actual task, then the answer to your question is pretty simple:
RewriteEngine on
RewriteRule ^/?post/(\d+)_ /blog/$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 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