301 Redirect url with query string? - .htaccess

I like to do the following redirection:
domain.de/?file=team ––> domain.de/team.html
To do this I try the following rewriting (that is'nt working):
RewriteCond %{QUERY_STRING} ^file=team$
RewriteRule ^/$ /team.html [L,R=301]
How can I do this 301 redirection?

Got the solution by my self:
RewriteCond %{QUERY_STRING} ^file=team$
RewriteRule ^$ /team.html? [L,R=301]
domain.de/?file=team ––> domain.de/team.html
If you see my question above I've made the following two mistakes:
1. Mistake: Missing Question Mark
If you remove the question mark then the query_string will not be removed:
RewriteRule ^$ /team.html [L,R=301]
domain.de/?file=team ––> domain.de/team.html?file=team
Just a further hint by the way the command RedirectMatch is not able to remove query_strings.
2. Mistake: Wrong Rewrite Path
RewriteRule ^\$ ... does not match doamin.de/?file=team
RewriteRule ^$ ... matches

Related

301 redirect from URL with GET-parameters to the homepage

I have an old website with Joomla 1.5. It has some strange links with GET-parameters, like this:
http://www.primavista.ru/images/stories/catalog/?rand=1186511674
http://www.primavista.ru/images/stories/catalog/?rand=145388433
http://www.primavista.ru/images/stories/catalog/?rand=1553907057
http://www.primavista.ru/images/stories/catalog/?rand=1563973527
http://www.primavista.ru/images/stories/catalog/?rand=1981273478
http://www.primavista.ru/images/stories/catalog/?rand=2139631800
http://www.primavista.ru/images/stories/catalog/?rand=366928750
http://www.primavista.ru/images/stories/catalog/?rand=524689684
http://www.primavista.ru/images/stories/catalog/?rand=569077423
http://www.primavista.ru/images/stories/catalog/?rand=573405687
http://www.primavista.ru/images/stories/catalog/?rand=879649167
I want make redirect theses links to the homepage.
I tried some different instructions in .htaccess:
RewriteCond %{QUERY_STRING} ^/images/stories/catalog/?rand=([0-9]*)$
RewriteRule ^(.*)$ https://primavista.ru/? [R=301,L]
RewriteCond %{QUERY_STRING} ^/images/stories/catalog/?rand=(.*)$
RewriteRule ^(.*)$ https://primavista.ru/? [R=301,L]
RewriteCond %{QUERY_STRING} (^|&)(rand)=[^&]+ [NC]
RewriteRule ^images/stories/catalog(/.*)?$ https://primavista.ru/? [R=301,L,NC]
But no one not working. Maybe here someone can help me with this. Thanks
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} rand=\d+
RewriteRule ^/?images/stories/catalog/?$ / [R=301,L]
It is a good idea to start out with R=302 temporary redirections and to only change that to R=301 permanent redirections one you are satisfied with everything. That prevents nasty caching issues on the client side ...
UPDATE:
Your comment below indicates that you actually ask to remove the GET parameter in the redirected request, which you never mentioned before...
You can use the additional QSD flag for that:
RewriteEngine on
RewriteCond %{QUERY_STRING} rand=\d+
RewriteRule ^/?images/stories/catalog/?$ /? [R=301,QSD,L]

Htaccess redirect with parameter?

I'm trying to do a .htaccess redirect with a parameter but it's not working. Seems like my regex are also wrong. :(
Original URL 1: http://www.example.com/?team=john-doe
Original URL 2: http://www.example.com/?team
Target URL: http://www.example.com/company/
I tried:
RewriteEngine on
RewriteCond %{QUERY_STRING} team=john-doe
RewriteRule /company/ [L,R=301]
Any help would be much appreciated.
Found a generator that works perfectly:
https://donatstudios.com/RewriteRule_Generator
# 301 --- http://www.example.com/?team=john-doe => http://www.example.com/company/
RewriteCond %{QUERY_STRING} (^|&)team\=john\-doe($|&)
RewriteRule ^$ /company/? [L,R=301]
Your RewriteRule is malformed, you are missing a pattern (first argument). Try something like:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^team(?:=john-doe)?
RewriteRule ^$ /company/? [R=301,L]
The RewriteRule pattern ^$ matches requests for the domain root. The ? on the end of the RewriteRule substitution strips the query string from the redirected URL (on Apache 2.4+ you can use the QSD flag instead).
The CondPattern ^team(?:=john-doe)? matches either "team=john-doe" (URL#1) or "team" (URL#2) at the start of the query string. The (?: part just makes it non-capturing.
You will need to clear your browser cache before testing.

Redirect all subdirectory child pages to subdirectory root

I've tried to find the answer to the question above and have been unable to do so. Most examples I've found are for redirecting all subdirectory child pages to the root, but not to the subdirectory's root.
For example, I want all http://domain.com/subdirectory/* to go to http://domain.com/subdirectory/ and have implemented the following redirect rule in .htaccess:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^subdirectory/(.*)$ /subdirectory/ [R=301,NC,L]
I've also tried:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^subdirectory/(.*)$ http://domain.com/subdirectory/ [R=301,NC,L]
Both scenarios above causes a redirect loop error when attempting to go to http://domain.com/subdirectory -- any ideas or suggestions here?
You need to use .+ after directory name in your regex:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(subdirectory)/(.+)$ /$1/ [R=301,NC,L]
Otherwise subdirectory/.* also matches /subdirectory/ and caused redirect loop.
For me the answer was not working at all, but this had:
RedirectMatch 301 ^/subdirectory/(.+)$ /subdirectory

htaccess: Redirect links with question mark a the end

I have a few links that look like this, that unfortunately are being used:
http://www.example.com/page/?
http://www.example.com/another-page/??
Is it possible to have them redirect without the question mark(s), or strip them out?
I've tried:
RewriteRule /page/? http://example.com/page/ [R=301,L]
RewriteRule /page/\? http://example.com/page/ [R=301,L]
RewriteRule ^/page/\? http://example.com/page/ [R=301,L]
RewriteRule ^/page/\?$ http://example.com/page/ [R=301,L]
Redirect 301 /page/? http://example.com/page/
You can't match against the query string (or even the ?) in a rewrite rule. Since the query string itself is empty, you can't even match against the variable, %{QUERY_STRING}. What you need to do is match against the actual request:
RewriteCond %{THE_REQUEST} \ /+([^\ \?]+)\?(\ |$)
RewriteRule ^(.*)$ /$1? [L,R=301]
It's going to look real weird with a ? at the end of the rule's target, but that's how you tell mod_rewrite to remove the query string.

mod_rewrite do not change links when rewrite

I have the following to my .htaccess
RewriteEngine on
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
When I call www.mydomain.com/page/myvalue I get the correct result. In order to to that though I'll need to change all links to my website correct?
Is there a way to accomplish the opposite i.e. when I call index.php?page=$1 to redirect me to www.mydomain.com/page/myvalue ?
Add this rule to your .htaccess:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ page/%1? [R=301,L]

Resources