Htaccess : Redirect url with get parameters - .htaccess

How can i redirect url with get parameters to a regular url :
from
/index.php?cid=100&id=550&Itemid=1084
to
/my-page
I tried :
Redirect 301 /index.php?cid=100&id=550&Itemid=1084 /my-page
but i didnt work

If, as you claim in your comment above, do not need the query arguments in the target request, you can do a simple redirection:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^cid=100&id=550&Itemid=1084$
RewriteRule ^/?index.php$ /my-page [R=301]
That rule will work in the http servers host configuration and likewise in a dynamic configuration file (.htaccess style file).
If you want the matching pattern to be somewhat more flexible, so to accept arbitrary numbers, not just exactly those you specified in the question, then you can use a slightly modified condition:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^cid=\d+&id=\d+&Itemid=\d+$
RewriteRule ^/?index.php$ /my-page [R=301]
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Related

From shop.domain.com to www.domain.com (.htaccess - mod_rewrite)

I can do this with mod_rewrite?
Example 1
From Url shop.domain.com/dir/dir1/Products/skuproduct123456 to www.domain.com/products/skuproduct123456
Example 2
From Url shop.domain.com/dir/dir1/pages/namepage/123456 to www.domain.com/collections/namepage-123456
Thank You
What you ask sounds pretty straight forward. You just have to adapt any of the many existing examples you could have found here...
There are two alternatives, however:
First the usual thing is to externally redirect such requests:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^shop\.example\.com$
RewriteRule ^/?dir/dir1/Products/skuproduct123456 https://www.example.com/products/skuproduct123456 [R=301,L]
RewriteCond %{HTTP_HOST} ^shop\.example\.com$
RewriteRule ^/?dir/dir1/pages/namepage/123456 https://www.example.com/collections/namepage-123456 [R=301,L]
It is a good diea to start out with a R=302 temporary redirection and only change that to a R=301 permanent redirection once everything works as expected.
Then it could also be that you actually want to internally rewrite those requests... That is a bit more complex. If both hosts are served by the same http server things are easy again, assuming that both hosts share the same DOCUMENT_ROOT (otherwise you need to adapt the paths accordingly):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^shop\.example\.com$
RewriteRule ^/?dir/dir1/Products/skuproduct123456 /products/skuproduct123456 [END]
RewriteCond %{HTTP_HOST} ^shop\.example\.com$
RewriteRule ^/?dir/dir1/pages/namepage/123456 /collections/namepage-123456 [END]
If however both hosts are served by separate http servers, the only option you have is to use the proxy module. That is also possible from within the rewriting module. This comes with a massive performance penalty though because of how network routing is setup in that case...
All approaches can be implemented not only for static, literal paths you implement but also in a more generic way using regular expressions as patterns. The exact rule depends on the actual structure of your URLs in that case, which you did not really name.
In general it is a good idea to implement such rules in the actual http server's host configuration. You can also use a distributed configuration file (often named ".htaccess"), but that comes with disadvantages. It is only offered as a last means for situations where you do not have control over the http server configuration (read: cheap web hosting providers).

Mod-rewrite rule which just gets the part of the URL after the =

I'm currently trying to redirect this URL
http://dev.example.org/active/researchers/contact.php?IDENT=12345
to
http://portaldev.example.org/users/ident/12345
in htaccess.
However, I can only get a redirect to
http://portaldev.example.org/users/ident/IDENT=12345
because I can't find a way to get rid of the IDENT=. How can I do that?
The rewrite conditions in my htaccess are:
RewriteCond %{REQUEST_URI} ^/active/researchers/contact\.php$
RewriteCond %{QUERY_STRING} ^IDENT=([0-9]*)$
RewriteRule ^(.*)$ http://portaldev.example.org/users/ident/$2 [R=302,NC,L]
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)IDENT=(\d+)(?:&|$)
RewriteRule ^/?active/researchers/contact\.php$ http://portaldev.example.org/users/ident/%1 [R=302,QSD,NC]
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).
Got the answer to my question for anyone else who needs help:
RewriteCond %{REQUEST_URI} ^/active/researchers/contact\.php$
RewriteCond %{QUERY_STRING} ^IDENT=(.*)
RewriteRule (.*) http://portaldev.cepr.org/users/ident/%1? [R=301,L]
I think the main thing was the ? at the end of the rewrite rule but it also wouldn't work unless I put in the first RewriteCond to request uri and used (.*) in the rewrite rule

htaccess url with regex to forbidden

I need to block all request made with a specific url like
http://www.domain.est/-p-.html?slave_id=15265&osCsid=6j0ltvo8d9i8h30koahqusvua7
I tried with
RewriteCond %{THE_REQUEST} /(\/-p-\.html)+(\?slave_id\=\d{1,6})?(\&osCsid\=\w{1,26})? [NC]
RewriteRule ^ - [F]
but doesn't work.
Where I'm wrong ?
Thanks
This is a version that is easier to handle and should be more robust too:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/-p-\.html$ [NC]
RewriteCond %{QUERY_STRING} slave_id=[0-9]{1,6}
RewriteCond %{QUERY_STRING} osCsid=[0-9a-z]{1,26}
RewriteRule ^ - [F]
Note: the query args matching patterns are not absolutely precise, but should be robust enough for almost all situations...
For this to work the http servers rewrite module has to be enabled, obviously. The rules will work in the http servers host configuration or in dynamic configurtation files (.htaccess). In case you decide to use such a dynamic configuration file you also have to take care to enable the interpretation of such files with the AllowOverride directive in the host configuration and the file has to be located in your http hosts configured DocumentRoot folder.
And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Regular redirects in .htaccess

I need a set redirect after word articles
For example there URN /articleswinter/howtodo/
I need after articles put /, If there is empty, as a result we should have
/articles/winter/howtodo/
thanks
Should be as easy as a simple rewrite rule:
RewriteEngine on
RewriteRule ^/?articles([^/].*)$ /articles/$1 [L,NC,QSA]
An alternative would be to use an explicit rewrite condition:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/articles/
RewriteRule ^/?articles(.*)$ /articles/$1 [L,NC,QSA]
And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

.htaccess url redirect and append .html only to asp pages

I am archiving a dynamic site by localising it into a static version and hosting it in a subfolder of a new domain.
http://oldsite.com/main.asp to
http://newsite.com/v4/main.asp.html
There was plenty of documentation explaining how to make a redirect:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^oldsite\.com$
RewriteRule (.*) https://newsite.com/v4/$1 [R=301,L]
But I'm struggling to work out the grep for adding .html to any file ending in .asp.
If the code you posted works except for the issue with the filename extension, then you can simply modify it like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example.org\.org$
RewriteRule ^(.*)\.asp$ https://example.org/v4/$1.html [R=301,NC,L]
This obviously does not handle any query parameters. But that won't work anyway when turning a dynamic site into a static one as you try, so I assume that simply does not matter...
And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Resources