.htaccess rewriterule not working properly - apache

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.

Related

RewriteRule to capture whole search string produces empty result

I have the following rule in .htaccess:
RewriteRule ^order(.*)$ /index.php?p=order&product=$1 [L,NC]
It's a simplification because later on I want to add order\?product=(.*)
I access the website with:
http://website.com/order?product=L0231-03868 but in the $_GET I'm only getting this:
Array ( [p] => skonfiguruj-zamowienie [product] => )
The product is empty. What am I missing?
-- edit
the moment I add the question mark
RewriteRule ^order\?(.*)$ /index.php?p=order&product=$1 [L,NC]
I get 404
Your URI doesn't really have anything after order so there is nothing to capture in (.*).
Use QSA flag to append original query string after rewrite.
No need to repeat order on both sides.
Suggested rule:
RewriteRule ^(order)/?$ index.php?p=$1 [L,NC,QSA]
Because of QSA flag, your original query string product=L0231-03868 will be appended to p=order and you will get both parameters in php file.
Note about patter ^order\?(.*)$ generating 404. Remember that a ? will never be part of URI to be matched using RewriteRule hence this pattern containing a ? is guaranteed to always fail.
With your shown samples please try following htacces rules file. Make sure to keep your htaccess rules file along with index.php file, order folder(3 of them should reside inside root directory). Also clear cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(order)\?(product)=(\S+)\s [NC]
RewriteRule ^ index.php?p=%1&%2=%3 [QSA,L]
To make it more Generic, as above rules are very samples specific only:
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/([^?]*)\?([^=]*)=(\S+)\s [NC]
RewriteRule ^ index.php?p=%1&%2=%3 [QSA,L]
Documentation link:
%{THE_REQUEST} is being used here, which contains complete request line.

.htaccess rewrite urls with parameters

I am trying to rewrite my urls through a .htaccess file to make them more clean looking. I have
http://localhost:801/Test/test.php?school=19&name=Greenhaven-Elementary
and it needs to end up looking like
http://localhost:801/Test/test.php/19/Greenhaven-Elementary
In my .htaccess file I have the following
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/+]+)([0-9]+)$ test.php?school=/$1&name=$2/ [L]
I have tried other ways but being new at using .htaccess files I haven't been able to figure it out.
This should do what you're after:
RewriteEngine On
RewriteCond %{QUERY_STRING} school=(.+)&name=(.+) [NC]
RewriteRule ^(.*)$ http://localhost:801/Test/test.php/%1/%2? [R=301,NC,L]
So what does the above do?
First, it will take the query school= and name= as a condition, if this condition is met then it will grab any version of the variables using (.+).
It will then rewrite the URL using 301 redirection to show http://localhost:801/Test/test.php/anything/anything2. The use of %1 and %2 is to grab the variables from school= / name= and then we use ? to stop the original query string from appearing on the end of the newly rewritten URL.
Make sure you clear your cache before testing this.
EDIT:
I wrote this for the singular query:
RewriteEngine On
RewriteCond %{QUERY_STRING} item=(.+) [NC]
RewriteRule ^(.*)$ http://localhost:801/Test/%1? [R=301,NC,L]
This includes removing test.php and on my server works without issue and returns http://localhost:801/Test/anything

Apache Mod_Rewrite empty query string followed by slash

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]`

Modrewrite and PHP issues

I am trying to write this rewrite code and it just is not working (apache 2.2):
RewriteEngine on
RewriteBase /
RewriteRule ^account/activate\?token=(.*) index.php?route=account/activate&token=$1
I tried many different variations. After trying for about and hour and searching google I am stumped. This is where you come in!
Try this
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^account/activate$ index.php?route=account/activate&%1
Basically this maps the entire query string to %1 in the RewriteRule and you can then access your token from your php-script using good old regular $_GET['token']
The pattern of the RewriteRule directive is only tested agains the URI path. The query string can only be tested with the RewriteCond directive:
RewriteCond %{QUERY_STRING} ^token=(.*)
RewriteRule ^account/activate$ index.php?route=account/activate&token=%1
But there’s a simpler solution: Just set the QSA flag to get the query string automatically appended to the new query string:
RewriteRule ^account/activate$ index.php?route=account/activate [QSA]
Note: You just need to set the QSA flag if there is a query defined in the substitution. If not, the original query already will automatically appended.
I've used this:
RewriteEngine On
RewriteRule ^account/activate/?$ index.php?route=account/activate&%{QUERY_STRING}
RewriteRule ^account/activate/?/$ index.php?route=account/activate&%{QUERY_STRING}
And it works with this:
<?php
echo "route is: " . $_GET['route'];
echo "</br>";
echo "token is " . $_GET['token'];
My directory where my code resides in is called test.
These are my urls:
http://localhost:80/test/account/activate/?token=test
http://localhost:80/test/account/activate/?token=asd
You cannot match the parameters part of your URL with mod_rewrite.
The only way to test for parameter in your URL is to use a RewriteCond directive and testing the env variable QUERY_STRING

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