How can I change url parameter value using htaccess - .htaccess

I want to validate url parameters using htaccess, where a certain parameter needs to have a valid value. If the value for the parameter is invalid, I want to redirect to another page.
E.g.: www.mydomain.com/folder/page1?specialId=1a3b5c78
The value for the "specialId" parameter needs to be an 8 character string.
If the value is invalid (e.g.: 1a3b5c78x or abc) I want to redirect to www.mydomain.com/folder/page2

You can use in your htaccess:
RewriteEngine on
RewriteCond %{QUERY_STRING} specialId [NC]
RewriteCond %{QUERY_STRING} !specialId=[A-Z]{8}(?:&|$) [NC]
RewriteRule page1/?$ /folder/page2? [R=301,NC,L]

Related

htaccess redirect with query string not working

enter code hereI have a WordPress website.
I have URLs for affiliates that look like this:
https://example.com/folder/?ref=23432
https://example.com/folder/?ref=13442
etc.
I would like to redirect any URL that ends in ?ref= to another domain.
For example, https://example.com/folder/?ref= should redirect to https://example.org/product/
How can I do this? I appreciate your time.
I tried
Redirect 301 example.com/folder/?ref https://example.org/product/
Thank you #MrWhite. I tried the following with no success.
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)ref=
RewriteRule ^example.com https://www.example.org/product/$0 [R=302,L]
RewriteRule ^example.com https://www.example.org/product/$0 [R=302,L]
The RewriteRule directive matches the URL-path only (less the slash prefix). So this should be matching against folder/ (as per your example), not the hostname.
And the $0 backreference in the substitution string is not required here. So this should simply be:
:
RewriteRule ^folder/$ https://www.example.org/product/ [R=302,L]
If you do need to check the requested hostname (ie. example.com) - if example.com and example.org point to the same server - then you need a separate condition (RewriteCond directive). For example, the complete rule would then become:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{QUERY_STRING} (^|&)ref=
RewriteRule ^folder/$ https://www.example.org/product/ [R=302,L]
Note that the regex (^|&)ref= matches the ref= URL parameter anywhere in the query string, if there happened to be other URL parameters that preceded it.
Reference:
htaccess redirect URL with parameter when a special parameter exists
https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

Rewrite url with .htaccess based on GET parameters

I want to rewrite the following url pattern:
http://example.com/some/inner/page?param1=test&lang=en
becomes
http://example.com/en/some/inner/page?param1=test
. So basically I want to take the "lang" get parameter and put it at the begginning of url. I know I have to use RewriteRule in the .htaccess but what is the pattern that has to be written ? Note that I also need it to work for both ?lang=en and ?some-parameter=test&lang=en
You can use this rule as your top most rule in root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \?(.*&)?lang=([^&]+)&?(\S*)\sHTTP [NC]
RewriteRule ^ /%2%{REQUEST_URI}?%1%3 [R=301,NE,L]
This will remove lang= parameter from anywhere in query string and redirect using value of same parameter at the front.

add string at the end of URL using htaccess which has query string also

I want to change
domain.com/division1/index.php?members/maxmusterman.5
to
domain.com/division1/index.php?members/maxmusterman.5/#div
That is if the URL contains index.php?members, then I add /#div at the end of url. I tried this
RewriteEngine On
RewriteCond %{REQUEST_URI} index.php?
RewriteRule (.*) /%1/%{QUERY_STRING}&123 [L,QSA,R=301]
but it returns
domain.com/members/maxmusterman.5&123?members/maxmusterman.5
Note here that &123 is attached after URI before starting parameters. I researched htaccess QSA flag but I could not find a way to add a custom string at the end of the query string. How can I do that. Here I have used &123 for test purpose, actual requirement is adding /#div
To redirect
domain.com/division1/index.php?members/maxmusterman.5
to
domain.com/division1/index.php?members/maxmusterman.5/#div
.
You can use something like the following :
RewriteEngine on
RewriteCond %{QUERY_STRING} !loop=no
RewriteRule ^division1/index\.php$ %{REQUEST_URI}?%{QUERY_STRING}&loop=no#div [L,R,NE]
I added an additional perameter loop=no to the destination url to prevent infinite loop error .You can't avoid this as both your old url and the new url are identical and can cause redirect loop if you remove the RewriteCond and Query perameter.
NE (no escape ) flag is important whenever you are redirecting to a fragment otherwise mod-rewrite converts the # to its hex %23 .
solution #2
RewriteEngine on
RewriteCond %{THE_REQUEST} !.*loop=no [NC]
RewriteCond %{THE_REQUEST} /division1/index\.php\?(.+)\s [NC]
RewriteRule ^ /division1/index.php?%1&loop=no#div [NE,L,R]
Clear your browser cache before testing these redirects.

301 Redirects with mod_rewrite

I'm working with mod_rewrite under .htaccess, and I'm trying to redirect (R=301) an URL like this :
http://domain/index.php?folder=AB_CD
to an URL like this
http://domain/AB/CD/
How can I write the rule please ?
Try the following code in root/.htaccess :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^folder=([^_]+)_([^&]+)$ [NC]
RewriteRule ^index\.php$ http://domain.com/%1/%2/? [NC,L,R]
Explaination :
RewriteCond %{QUERY_STRING} ^folder=([^_]+)_([^&]+)$ [NC]
Checks to ensure that the url (index.php) has query strings with specific key and value, ( folder=foo_bar) acording to the regex pattern, if the url has valid query strings then the rule is processed
RewriteRule ^index\.php$ http://domain.com/%1/%2/? [NC,L,R]
index.php?query_strings gets redirected to /query/strings, if the condition is met.
Empty question mark ? at the end of the Rewrite target is important as it discards the orignal query strings, without it /index.php?folder=foo_bar redirects to /foo/bar/?folder=foo_bar appending the old query strings.
(Hope, this helps!)

htaccess, mod_rewrite, how to check for empty get var?

Breaking my head on this one..
Need to check if a get variable named 'filter' has value or not with mod_rewrite..
http://site.com/audio/speakers?filter= should redirect to: http://site.com/audio/speakers
http://site.com/audio/speakers?filter=yes shouldn't redirect..
Thanks!
You can try this:
RewriteCond %{QUERY_STRING} ^filter=$ [NC]
RewriteRule ^audio/speakers$ http://site.com/audio/speakers? [R=302,L]
It will only check for this URL: /audio/speakers?filter= (where filter is first and only one parameter and is empty) -- exactly like in your URL example. If there will be more than one parameter .. it will not match and will not do anything (even if filter will be empty).
You can change redirect code form 302 (temp) to 301 (permanent) if required.
UPDATE:
RewriteCond %{QUERY_STRING} ^filter=$ [NC]
RewriteRule ^(.*)$ http://site.com/$1? [R=302,L]

Resources