htaccess Rewrite Rules appending parameter? - regex

Our original urls began with a parameter and the following rewrite works to redirect them (thanks to Jon Lin).
However, the original parameter is being appended to redirect, so that
RewriteEngine On
RewriteCond %{QUERY_STRING} ^old-page$
RewriteRule ^$ /newpage [R=301,L]
ends up going to mydomain.com/newpage?old-page
Any idea how to fix? thanks again,
Geoff

Have it like this to strip off existing query string:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^old-page$ [NC]
RewriteRule ^$ /newpage? [R=301,L]
Take note of ? at the end of target URI, that is used to strip-off existing query string.

Related

Handle one query parameter specifically

I need to handle one query parameter specifically.
The url can look like the following:
https://example.com/?app=egov&map=1337
https://example.com/?app=egov&map=1337&showoptions=true
https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorld
The redirect should point to
https://example.com/contextName/resources/apps/egov/index.html?map=1337
https://example.com/contextName/resources/apps/egov/index.html?map=1337&showoptions=true
https://example.com/contextName/resources/apps/egov/index.html?map=1337&anotherparam=helloWorld
As you can see, the app-Parameter needs to be used as an URL-part; the others need to be used like traditional query-params.
I've figured out, how to implement the first part, where https://example.com/?app=egov points to https://example.com/contextName/resources/apps/egov/index.html using the following
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^app\=(.+)
RewriteRule ^/$ "/contextName/resources/apps/%1/index.html" [R,L,QSA]
but I'm struggeling how to realize the correct handling of the other params.
All these combination of query string can be handled by a single rule loke this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^app=([\w-]+)(?:&(.*))?$ [NC]
RewriteRule ^$ contextName/resources/apps/%1/index.html?%2 [L]
RewriteCond matches app query parameter and captures it in %1 while rest of the query string after & is captured in %2.
With your shown samples, please try following htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
####Apache documentation link https://httpd.apache.org/docs/trunk/rewrite/remapping.html for more info.
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2 [L]
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337&showoptions=true
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=(.*)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2&showoptions=%3 [L]
##Internal rewrite rule for https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorl
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=([^&]*)&anotherparam=(.*)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2&anotherparam=%4 [L]
All of your URLs uri part is NULL, so rules are written based on that.

How to create .htaccess for multiple parameters in URL?

I need using this .htaccess file. It only changes the URL from www.mydomain.com/users/user?usernam=myname123&profile=myprofile123&data=mydata123 to www.example.com/users/myname123/myprofile123/mydata123 .
i use
RewriteEngine On
RewriteRule ^user\.php$ - [L]
RewriteRule ^([0-9a-zA-Z\-_.]*)/?$ /users/user.php?username=$1&profile=$2&data=$3[L,QSA]
htaccess in users folder
not work
With your shown samples, could you please try following rules. This will take anything in REQUEST_URI starting.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{THE_REQUEST} /([^/]*)/([^/]*)/([^/]*)/([^/]*)/?\s
RewriteRule ^(.*) /%1?username=%2&profile=%3&data=%4 [NE,L,NC]
OR(either use above or following one) in case you want to match users in URI then run rules then try following.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{THE_REQUEST} /users/([^/]*)/([^/]*)/([^/]*)/?\s
RewriteRule ^(.*) /users?username=%1&profile=%2&data=%3 [NE,L,NC]
Explanation: Adding detailed explanation for above.
RewriteEngine ON: Enabling RewriteEngine here to enable mod rule writing.
RewriteCond %{QUERY_STRING} ^$: Checking condition if query string is NULL then go further in rules.
RewriteCond %{THE_REQUEST} /users/([^/]*)/([^/]*)/([^/]*)/?\s: Matching regex in THE_REQUEST from /users to till spaces and capturing 3 groups values into back references to be used later.
RewriteRule ^(.*) /users?username=%1&profile=%2&data=%3 [NE,L,NC]: Using url rewrite to change URI to as per OP's request which has back references values in it.

301 redirect with query parameters and ~ in URL

I have a redirect problem where I would like to redirect a url with query parameters to another PDF entirely, e.g.:
Original URL:
https://www.foo.bar.com/~/bar.pdf?la
Redirect URL:
https://www.bar.foo.com/foo.pdf
Currently my rewrite rule is as shown below:
RewriteRule ^/~/bar.pdf?la https://www.bar.foo.com/foo.pdf [R=301,L,NC]
How can I write a 301 redirect that both accounts for the tilde and the query parameter?
Thanks in advance for your help!
First your rule RewriteRule ^/~/bar.pdf?la https://www.bar.foo.com/foo.pdf [R=301,L,NC] has several issues .
First , RewriteRule ^/ it is not applicable here so RewriteRule ^ is enough.
Second , you should escape ~ like this RewriteRule ^\~
and finally , dont't write query string with RewriteRule like bar.pdf?la because it is not a pert of URI.
The line should look like this :
RewriteRule ^\~/bar.pdf https://www.bar.foo.com/foo.pdf [R=301,L,NC]
But the query string may append to new target , to prevent that add ? at the end of target this :
RewriteRule ^\~/bar.pdf https://www.bar.foo.com/foo.pdf? [R=301,L,NC]
If you want to use query string value , you may add condition before that to utilize it:
RewriteCond %{QUERY_STRING} ^(.*)$
you could change ^(.*)$ to whatever you want and you could use it in target like this example :
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ https://www.whatever.com/%1/$1 [R=301,L,NC]
So , %1 refer to this condition RewriteCond %{QUERY_STRING} ^(.*)$ and $1 refer to this condition RewriteRule ^(.*)$ and so on.
You need to use RewriteCond
directive to check the url query string. You can not do that in RewriteRule . The querystring in your url is la .
RewriteEngine on
RewriteCond ℅{QUERY_STRING} ^la$ [NC]
RewriteRule ^.*bar\.pdf$ /foo.pdf? [L,R=301]

htaccess RewriteRule regex

I have hundreds of these old links I need to redirect.
Here is one example:
/index.php?option=com_content&view=article&id=433:seventh-character-code-categories-and-icd-10-cm&Itemid=101&showall=1
to
/seventh-character-code-categories-and-icd-10-cm
Essentially I need to remove the /index.php?option=com_content&view=article&id=433: part.
I tried this but I am getting confused with the [0-9] and : parts, so the following does not work:
RewriteRule ^/index.php?option=com_content&view=article&id=[0-9]:(.*)$ /$1 [L,R=301]
Say you want to capture from after : to right before & in the query string you mentioned, then try this expression:
^[^\:]*\:([^\&]*)\&.*$
As #starkeen mentioned in comments, you got to check against the query string. This can be done using RewriteCond %{QUERY_STRING}
So if index.php is in the root folder:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/index\.php$
RewriteCond %{QUERY_STRING} ^[^\:]*\:([^\&]*)\&.*$
RewriteRule ^(.*)$ http://example.com/%1 [R=301,L]
Here's another example. This one is for a sub folder:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/pages\/index\.php$
RewriteCond %{QUERY_STRING} ^[^\:]*\:([^\&]*)\&.*$
RewriteRule ^(.*)$ /pages/%1? [R=301,L]
Also, notice the ? at the end of the url /pages/%1?, this prevents from re-attaching the query string.
Another thing, captured groups will be set to variables %{number} since set in the RewriteCond.
BTW, depending on your server's configuration, you may need to add the NE flag, like [NE,L,R=301] Plus test whether it is necessary to double escape the literal characters.
what is about direct approach. Skip all till semicolon, mach string till & and replace all with first much
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} [^:]+:([\w-]+[^&]).*
RewriteRule .*$ \/%1? [R=301,L]
</IfModule>

Rewrite URL when parameter is missing

I'm looking for an Apache Rewrite Condition that fires if a certain URL parameter is not set and then rewrites the URL by adding this parameter.
So basically if my URL looks like http://www.heco.de/index.php?id=123&catId=456, I want it to redirect to http://www.heco.de/index.php?id=123&catId=456&cHasH=456
The first parameter is a static page ID, but the catId varies from a single digit up to 6 digits.
I tried to achieve this by adding this code snippet to my .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} catId=([0-9]{1,6})
RewriteCond %{QUERY_STRING} !cHash=
RewriteRule ^(.*)/?id=123&catId=([0-9]{1,6})$ index.php?id=123&catId=$2&cHash=$2
#RewriteRule ^(.*)/?id=123&catId=([0-9]+)$ http://www.stackoverflow.com
Alas, it's not working. I tried testing this by uncommenting the last line, but no redirect takes place at all. Hence I would be really grateful for any insights, ideas or further advice...!
Thx in advance,
Martin
You can't match QUERY_STRING in RewriteRule.
You can use this rule:
RewriteCond %{QUERY_STRING} !(?:^|&)cHash=[^&]+ [NC]
RewriteCond %{QUERY_STRING} (?:^|&)catId=([0-9]{1,6}) [NC]
RewriteRule ^index\.php$ $0?cHash=%1 [L,QSA,NC]