Apache Mod_Rewrite empty query string followed by slash - apache

I have some super weird URLs I need to redirect. The original URLs look like this:
server1.com/directory?/tfoo
These URLs needs to go here:
server2.com/search~?query=foo
I've tried a bunch of different possibilities, but this is what I'm working with right now:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/t(.*) https://server2.com/search?query=$2 [L,NE,NC]
Basically, I thought I would have to look for a blank query string, and then try to move on to grab the value given in the directory structure (foo). The RewriteCond matches my original URL, but I can't seem to grab the actual query parameter.
I've tried a bunch of things with RegEx to try to ignore the questionmark altogether, but it looks like mod_rewrite only understands the questionmark in the context of a query parameter redirect.
Any advice is hugely appreciated.
Thanks!

You can use this :
RewriteEngine on
RewriteCond %{THE_REQUEST} /directory/?/t(.+)\s [NC]
RewriteRule ^ http://server2.com/search~?query=%1 [NE,L,R]

Deadooshka's solution (commented above) worked:
RewriteCond %{QUERY_STRING} "^/t(.*)$"
RewriteRule "^/?directory$" "https://server2.com/search?query=%1" [L,NE,NC]`

Related

htaccess rule to remove parameters

struggling to come up with a .htaccess rule to remove parameters from it.
Essentially I want
http://www.example.com/page/competition.html?utm_medium=email&utm_campaign=11&utm_source=11&utm_term=11.gif
to rewrite to
http://www.example.com/page/competition.html
so, anything right from the /page/compeition.html is rewritten.
Grateful for any assistance. thanks in advance
In your question, you state that you want the long URI rewritten to the shorter one. Unfortunately, that isn't very clear. Assuming that you wish to simply remove the query string using a redirect, you can use the following:
RewriteCond %{QUERY_STRING} ^utm_medium=email&utm_campaign=([^&]+)&utm_source=([^&]+)&utm_term=([^&]+)$
RewriteRule ^ %{REQUEST_URI}? [R=302,L]
This is a generalised condition and rule that will check for any utm information in the query string. If it is present, then strip out the query string.
If you wish to only do this for page/competition.html, then change the rule to this:
RewriteRule ^(page/competition\.html)$ /$1? [R=302,L]
If you wish to simply remove any query string forming part of a request to page/competition.html, then you can use this instead of the above:
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(page/competition\.html)$ /$1? [R=302,L]
This basically tells Apache to check if the query string is not empty and, if it isn't, strip it out altogether.
To make the redirect permanent, change 302 to 301.
As a general rule, you should be able to use the following:
RewriteRule ^(.*)$ $1? [END,QSA]
The key in this rule is the trailing questionmark which will not appear in the output URL.
RewriteRule ^(.*)$ $1? [END,QSA] This rule was met, the new url is
http://www.example.com/page/competition.html
Try it out at the following:
http://htaccess.madewithlove.be/
Do you want that only for the specific page? Or more as a general approach?
RewriteRule ^page/competition.html?(.+)$ /page/competition.html [L,NC]
That would be the very easiest form to achieve it, but would only work for the exact filename. For a more general approach, you would need to add some more placeholders.

.htaccess rewriterule not working properly

I'm trying to mask a URL that looks like this:
http://example.com/test-test-test-test/item.swf?id=122
...to display the content of this URL, while passing on the GET parameter:
http://example.com/images/item.swf?id=122
This is because item.swf does not actually exist in the test-test-test-test directory. I'm currently using this, however it is not working:
RewriteRule ^test-test-test-test/item\.swf\?id=([0-9]+)$ images/item\.swf\?id=$1 [L]
This .htaccess file is in my root folder.
Try this instead:
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^test-test-test-test/item\.swf /images/item.swf?id=%1 [R,L]
RewriteRule regex isn't on query parameters, for this, you can use the [QSA] flag:
"When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined."
Example :
RewriteRule ^test-test-test-test/item\.swf$ images/item.swf [L,QSA]
Hope it helps.

How to redirect direct hits

I'm sure this has been asked before, but I have no idea what it would be called.
I have done a quick Google and search here on SO.
Perhaps it would be better asked, how do I redirect http://site.com/shop to a specified URL like http://othersite.com/page without it affecting http://site.com/shop/?q=test or any other $_GET's.
RewriteRule ^shop http://othersite.com/page [R=301,L]
Works fine apart from it also affects links like http://site.com/shop/?q=test which I want to work.
Cheers.
You can use mod_rewrite to match against the query string (or to see if there is no query string):
RewriteEngine On
# blank query string
RewriteCond %{QUERY_STRING} ^$
# check host
RewriteCond %{HTTP_HOST} site\.com$ [NC]
RewriteRule ^shop/?$ http://othersite.com/page [R=301,L]

.htaccess rewrite rule for redirects

I've been fighting this for a while and can't seem to make it work. My old system used a lot of query strings to render pages but they are no longer necessary. My url is below:
OLD URL: www.example.com/links.php?section=5&catid=52
NEW URL: www.example.com/mhfs/links
The name links is coincidental and not necessarily from the old pages name. I need to check which section and catid is present and redirect them to the appropriate page from what it is. I tried the following but this just seems to do nothing. What am I doing wrong?
RewriteCond %{REQUEST_URI} ^links.php$
RewriteCond %{QUERY_STRING} ^section=5$
RewriteCond %{QUERY_STRING} ^catid=52$
RewriteRule ^(.*)$ /mhfs/links? [R=301]
Any help would be GREATLY appreciated.
You must turn the rewrite engine on for it to work.
You probably don't want the regex start symbol on the replacement:
RewriteEngine on
RewriteCond ...
RewriteRule ...

mod_rewrite weird problem

I have a strange problem with mod_rewrite, the rules that are relevant here are:
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/igre\-(.*)\.php$ game.php?GameUrl=$2&Page=1 [L]
And a corresponding URL might look something like this:
example.com/miselne-igre/igre-shirk.php?Page=2
example.com/miselne-igre/igre-shirk.php
The problem is that the first rule has no effect. If I use the first URL from the example I always get 1 into the Page variable, which shows that the second rule is used.
So what's wrong with the first one? And why is the second rule even matching a URL with ".php?Page=XYZ" at the end, if I said that the URL ends with ".php"?
ps: other rules in the .htaccess file are working fine...
The query string is not part of the URI path that is being processed by the RewriteRule directive. You have to use the RewriteCond directive to process the query string.
RewriteCond %{QUERY_STRING} ^Page=[0-9]+$
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&%0 [L]
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&Page=1 [L]
But you can still simplify this by using the QSA flag (query string append):
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1 [L,QSA]
mod_rewrite is not using the query in it's rewriting process. Therefor you first RewriteRule is ignored. You could combine it with a RewriteCond (haven't tested it though) like so:
RewriteCond %{QUERY_STRING} Page=([0-9]+)
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2 [L, qsappend]
# qsappend appends the original query, in this case (Page=xx)
Ah, like Gumbo said; you can also use %1 to back reference to the page numer.
Is it just me or are your arguments back-to-front?
Do you mean:
RewriteRule ^(.*)\/(.*)\-igre\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/(.*)\-igre\.php$ game.php?GameUrl=$2&Page=1 [L]
You wanted to match miselne-igre not igre-miselne.
Obviously this doesn't address the main issue, but thought I'd throw that in.
Dom