I am not able 301 redirect domain.tld/?cur=usd to domain.tld - .htaccess

I try to redirect domain.tld/?cur=usd to domain.tld (there are many curencies, this is only example of one currency - we do not use anymore this solution).
I need to redirect only home with parameter to home without parameter. The other urls worked for me, I'm just having trouble getting work with that one.
I try to search and use online generators but none of the solutions work.
Here is what I am trying:
RewriteCond %{QUERY_STRING} (^|&)cur\=(.*)($|&)
RewriteRule ^$ /? [L,R=301]
// update
before this rule I have only
#bof redirects
RewriteEngine enabled
...and then there are redirects for other URLs, but I tested this rule separately first and the result was the same...
It not redirect me.
Thanks for the help and maybe an explanation of what I'm doing wrong.

RewriteCond %{QUERY_STRING} (^|&)cur\=(.*)($|&)
RewriteRule ^$ /? [L,R=301]
As mentioned in comments, this should already do as you require, providing there are no conflicts with other directives in the .htaccess file.
However, the regex in the preceding condition is excessively verbose for what you are trying to achieve (ie. just testing for the presence of the cur URL parameter).
If you simply want to check for the cur URL parameter anywhere in the query string then the regex (^|&)cur= would suffice (and is more efficient). No need to backslash-escape the literal =. And if the URL parameter always appears at the start of the query string then just use ^cur=.

I found the problem - it was something with the hosting, after a reboot everything started working as expected.
So I can confirm that this rule is fine.
Sorry for question.

Related

Replacing ? with # in url to avoid cache busting

I discovered the UTM variables for Google Analytics bust our page caching. Google Analytics allows using hash instead of question mark, so
http://www.example.com/?utm_source=source&utm_medium=medium
and
http://www.example.com/#utm_source=source&utm_medium=medium
Are actually the same. But, as I said, the first option busts our caching while the second does not.
When we generate our own links, then I use #. However, services like Feedburner or dlvr.it append the UTM campaign variables with a ? -- so I want in .htaccess to redirect all /?utm... to /#utm...
I know that NE (No Escape) should be used for the hash, but I honestly can't figure out how the rewrite condition and rule should be like.
The rule logic should be -
RewriteCond: if {path}?{query_string} AND {query_string} has 'utm_source' as a variable,
RewriteRule: redirect to {path}#{query_string} [R,NE,L]
Can anyone help me with this place?
OK, I got a eureka shortly after asking the question...
The solution is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} utm_source
RewriteRule ^ %{REQUEST_URI}#%{QUERY_STRING}? [R,NE,L]
</IfModule>
Tested and works...

Pattern Matching htaccess conflict

I have migrated my site to new software and am trying to ensure that older links are appropriately redirected to the new url's. I thought I had it working until I received the message from Google regarding increasing 404's.
I seem though to be causing a conflict between the new htaccess requirements and my changes to address the old links.
So the new links look like this:
http://www.exampledomain.com/search/?q=searchterm
And in the htaccess they are picked up like this:
RewriteRule ^search/(.*)$ search.php?q=$1
The above is working as it should.
The old links can look like either of these:
http://www.exampledomain.com/search/searchterm/
or
http://www.exampledomain.com/search/searchterm
I had put this in to the htaccess
RewriteRule ^search/(.*)/$ http://www.exampledomain.com/search.php?q=$1 [R=301,NC,L]
If I don't add the first rule then the new url's just bring up 404's.
If I add second rule the it stops the searchterm being passed and conflicts with the first rule.
Have tried a few things but think there must be an issue with the matching or something else i'm missing.
Any ideas appreciated.
** Added **
So after the first reply I made the change as suggested and it caused a couple of issues but switching the order of the rules has fixed that but has not quite fixed the issue
So now I have this:
RewriteRule ^search/(.*)/?$ http://www.exampledomain.com/search.php?q=$1 [R=301,NC,L]
RewriteRule ^search/(.*)$ search.php?q=$1
The above works for these url's now:
http://www.exampledomain.com/search/?q=searchterm
and
http://www.exampledomain.com/search/searchterm
But for url's like this:
http://www.exampledomain.com/search/searchterm/
it results in this with a trailing slash which prevents the search:
http://www.exampledomain.com/search.php?q=drama/
So just need to remove or not have the trailing slash
I think you are just missing a ? in the rule since the / is optional:
RewriteRule ^search/(.*)/?$ search.php?q=$1 [R=301,NC,L]
Update to address trailing slash:
I'm guessing that the .* is consuming the trailing / before the next rule. To fix that, we need to exclude it from the match:
RewriteRule ^search/([^/]*)/?$ search.php?q=$1 [R=301,NC,L]
Update to address added case where parameter appears after slash:
I'm not sure if you mean literally /search/q=searchstr or /search/?q=searchstr so I will attempt to address both cases.
If it's the latter, which is a true query string, place this rule above the first using QSA in order to pass the query string along to the new URL:
RewriteRule ^search/$ search.php [R=301,NC,QSA,L]
To address the first variation (without the ? query string), you will need to place this rule above the first, which literally looks for the q=:
RewriteRule ^search/q=(.*)$ search.php?q=$1 [R=301,NC,L]
And since I get the feeling you'll update this question again to ask about what happens if there is a trailing slash, I'll go ahead and modify that rule to handle this case as well:
RewriteRule ^search/q=([^/]*)/?$ search.php?q=$1 [R=301,NC,L]
If these rules still don't solve every case for you, then you're dealing with some really bad code from your previous URL and I feel very sorry for you. :P

Trying to redirect from domain.com/user/12345 to domain.com/user?id=12345

I'm trying to redirect from domain.com/user/12345 to domain.com/user?id=12345 and from domain.com/user/12345/profile to domain.com/user/profile?id=12345.
I've written this rule in .htaccess:
RewriteRule ^user/([0-9]*)(/.*)?$ user$2?id=$1 [L,QSA]
It works ok for domain.com/user/12345/profile but domain.com/user/12345 is not redirected.
I've also tried with this simplest form for this case:
RewriteRule ^user/([0-9]*)$ user?id=$1 [L,QSA]
I've tested both in http://htaccess.madewithlove.be/ and seems to work fine.
¿What is my mistake? ¿How can I do it?
UPDATE:
The next rule I've in this file rewrites from domain.com/user to domain.com/user.php and so on. If the previous rule is not defined it rewrites ok from domain.com/user/12345 to domain.com/user/12345.php but with the previous rule defined it neither does this rewriting.
Does it means that there is any kind or transformation that skip the second rule or that there is any kind of misyake that stops rules verification?
I've found the solution. I don't know why it worked well in a case and not in the other but I'd forgotten the slash so it was trying to rewrite to user?id=12345and not to /user?id=12345.
So the rule must be like this one:
RewriteRule ^user/([0-9]*)(/.*)?$ /user$2?id=$1 [L,QSA]

Subdomain redirect to URL Parameter while keeping other url params intact

I've looked like crazy for an answer to this - and I haven't been able to find one as of yet. My apologies in advance if I've missed something.
I'm looking to convert a subdomain to a url parameter while maintaining the other url pieces.
Such as:
http://sub.domain.tv/value redirects to http://domain.tv/value?campus=sub
This is what I've tried after looking through numerous posts on here:
# campus
RewriteCond %{HTTP_HOST} ^sub.domain.tv$ [NC]
RewriteRule ^(.*)$ http://domain.tv/$1/?campus=sub [R=301,L]
which worked fine for redirecting sub.domain.tv to domain.tv?campus=sub, but does not work with the rest of the URL parameters (so heading to sub.domain.tv/value does nothing).
I've tried other examples with the %1 and $1 being used as variables, such as with this post:
htaccess subdomain redirct with last url parameter but I don't understand the difference (and the examples I've tried did not work).
Thanks for taking a look! :) I very much appreciate it!
Have you tried the [QSA] parameter. This will add any remaining Query parameters to campus=sub

.htaccess and url

Is it possible somehow using mod_rewrite change the url from http://www.mywebsite.com/company/123/reviews to http://www.mywebsite.com/company-123/reivews?
It's not a redirect. The problem is that the real path is the first one and I need my browser to display the second path. So when the user goes to company-123/reviews the content of the page is displayed from company/123/reviews.
Thank you.
RewriteRule ^/([a-z]*?)-([0-9]*?)/([a-z]*?)$ /$1/$2/$3
I think this will work, the regex does it's job atleast.
Use this rule to rewrite the former URL path to the latter one:
RewriteEngine on
RewriteRule ^([^/]+)-([0-9]+)/([^/]+)$ $1/$2/$3 [L]
But you already need the former URLs in your documents to get this rewriting work.

Resources