Htaccess redirect depending on the value of variable - apache

I have some difficulties with such an easy problem.
On our site we've got a paginator, wich works with GET-variable "p" (p=1 - is the first page, p=2 - second and so on).
I do not want the script to consider the value 1 of this variable (p=1). So I've got the query string like:
http://www.mysite.ru/mypage/some_page2?p=1
and want to redirect user via htaccess to page:
http://www.mysite.ru/mypage/some_page2
It would be nice, if htaccess rule handle not only value "1", but also "","bla-bla-bla" like
http://www.mysite.ru/mypage/some_page2?p=$#^&*_not_right_value
http://www.mysite.ru/mypage/some_page2?p=
Thank you for your attention.
UPD:
The working solution for my case:
RewriteCond %{QUERY_STRING} ^p=1$
RewriteRule ^(.*)$ /$1? [R,L]

Query string manipulation via .htaccess can generally only be done via mod_rewrite. I haven't tested it, but the following should be close to what you need.
RewriteEngine on
RewriteCond %{QUERY_STRING} p=1 [OR]
RewriteCond %{QUERY_STRING} p=[^0-9]
RewriteRule (.*) http://www.mysite.ru/$1 [R, L]
The second condition simply tests that the p= is followed by a number. Once tested, the rule can be expanded to test that only a number exists after p (ie, right now p=2garbage would work) but it is better to start off with just the basics.

The working solution for my case:
RewriteCond %{QUERY_STRING} ^p=1$
RewriteRule ^(.*)$ /$1? [R,L]

Related

Modify query string value using htacces

I want to redirect
https://www.example.com/signup?plan=basic to https://www.example.com/signup?plan=basic-monthly and https://www.example.com/signup?plan=pro to https://www.example.com/signup?plan=pro-monthly .
How can I achieve this using htaccess ?
There are many questions related to this here. But, couldn't find an answer for this specific scenario.
This is the code I tried and failed:
RewriteCond %{QUERY_STRING} (^|&)plan=pro(&|$)
RewriteRule ^signup /$0?plan=pro-monthly [R=301,L]
Also, while trying the same with "basic" instead of "pro", the word "basic" i shown in red color as if it is a keyword.
Could you please try following, written with shown samples. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^(plan=(?:basic|pro))$ [NC]
RewriteRule ^(signup)/?$ $1?%1-monthly [L]
2nd solution: Or you could try following too. Make sure you either put 1st solution rules OR this one at a time.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(signup)\?(plan=(?:basic|pro))\s [NC]
RewriteRule ^ %1?%2-monthly [L]

Apache .htaccess rewrite parameter to directory

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

Escape string within RewriteRule

I am currently chewing on this problem: I am reworking a sort of CMS system, the result will be that URLs will look entire differently, but I want to save links and bookmarks, so I scrathced this .htaccess file together:
RewriteEngine on
# Rewrite old links to match with new URL syntax:
# universal rewrite
RewriteCond %{REQUEST_URI}~%{QUERY_STRING} ^(.*?\/*)? (viewuser|viewstory|reviews|news)\.php~(.+)$ [NC]
RewriteRule ^(.+).php$ %1?action=redirect&source=$1&%3 [R=301,L,NE]
And it works, http://example.com/news.php?action=newsstory&nid=51 actually becomes http://example.com/?action=redirect&source=news&action=newsstory&nid=51
But you can see the problem, a double action. Using 'action' is not the most inventive term, but it is what the script I need to feed is working with, and also what I am getting from the old one, so I need to either:
replace the second 'action' with anything else
or serialize/escape the entire part which is defined by %3 from, the RewriteCond
In case you are wondering, the question mark required the use of a RewriteCond, couldn't get it to work any other way, so any solution that gets rid of it is just as welcome.
Well, thanks for looking into and maybe even shedding a light onto things, I just made my way into the whole regex thing, but havewn't entirely figured how variables are passed from chained RewriteRule to another, maybe that would have been the way to got, but try and error didn't get me anywhere.
Try removing the NE flag and use a B:
RewriteCond %{REQUEST_URI}~%{QUERY_STRING} ^(.*?\/*)?(viewuser|viewstory|reviews|news)\.php~(.+)$ [NC]
RewriteRule ^(.+).php$ /?action=redirect&source=$1&%3 [R=301,L,B]
The only problem is that the %1 backreference will get encoded as well so I left that out. If you must have it there, you can try doing a two step rewrite:
RewriteCond %{REQUEST_URI}~%{QUERY_STRING} ^(.*?\/*)?(viewuser|viewstory|reviews|news)\.php~(.+)$ [NC]
RewriteRule ^(.+).php$ /special-rewrite-here?action=redirect&source=$1&%3 [L,B]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ (.*?\/*)?(viewuser|viewstory|reviews|news)\.php~(.+)$ [NC]
RewriteRule ^special-rewrite-here$ /%1 [L,R=301]
Fixed with help from Jon Lin:
RewriteCond %{REQUEST_URI}~%{QUERY_STRING} ^(.*?\/*)? (viewuser|viewstory|reviews|news)\.php~(.+)$ [NC]
RewriteRule ^(.+).php$ /special-rewrite-here?action=redirect&source=$1&old_data=%3 [B]
RewriteCond %{REQUEST_URI}~%{QUERY_STRING} ^(.*?\/*)? (viewuser|viewstory|reviews|news)\.php~(.+)$ [NC]
RewriteRule ^/.+$ %1 [L,R=301]
I had to do the RewriteCond again to preserve %1, and had to modify the second RewriteRule slightly, but he sure got me on the right track, so: problem solved, the rest will take place in PHP, that should be no problem then.

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]

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