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
Related
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
I've got an application that has been migrated to a newer platform. The tasks are similar and I'd like to redirect a GET parameter to a directory. For example
http://gallery/index.php?gal=ABC => http://photos/gal/ABC
and
http://gallery/?gal=DEF => http://photos/gal/DEF
and the anything that doesn't get caught redirect it to http://photos
I've tried
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^(/index.php)$ /%1/%2?
However all I get is a 404 and no redirection. Similarly, I've tried
RedirectMatch ^/index\.php\?=gal(.*) http://photos/gal/$1
but I'm having trouble escaping the ? in the original URL.
What would be the proper way of going about this?
Create a .htaccess file and insert the following code:
RewriteCond %{QUERY_STRING} ^(.+)=(.+)$
RewriteRule ^index.php$ http://photos %1/%2? [L,NC,R=301]
Your order is reversed. Rewrite it in front of you
RewriteRule /(.+)\/(.+) index.php?$1=$2
The question is old but might be still relevant for others, so I suggest a slightly different general approach:
RewriteEngine On
RewriteCond %{QUERY_STRING} key=([0-9a-zA-Z]+) [NC]
RewriteRule (.*) /%1? [R=302,L]
Notes:
QUERY_STRING in the condition checks for a param name "key" and catches it's value
The rewrite rule adds the param value as directory using %1. Note the ? removes the original query part from end result
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.
I need to create an alias for one exact page which unfortunately special characters in it. I do have an environment set up so other redirects work fine, but not this one:
RewriteRule ^/ow/email.htm?who=Kate%20Jones&direct=True&directemail=kate.jones#google.com$ http://www.google.com/ow/lalala.htm
How should I rewrite this statement so that it works?
PS. This is my first time here so do let me know if I'm not following stackoverflow policy correctly or smth ;) Thank you so much!
You need to use the RewriteCond directive for the URI query, either to examine QUERY_STRING:
RewriteCond %{QUERY_STRING} =who=Kate%20Jones&direct=True&directemail=kate.jones#google.com
RewriteRule ^/ow/email\.htm$ http://www.google.com/ow/lalala.htm
Or the request line in THE_REQUEST:
RewriteCond %{THE_REQUEST} ^GET\ /ow/email\.htm\?who=Kate%20Jones&direct=True&directemail=kate\.jones#google.com\s
RewriteRule ^/ow/email\.htm$ http://www.google.com/ow/lalala.htm
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