rewrite condition for directory along with file - apache

Any one please help me.I am new to .htaccess
I want to check the following condition
RewriteCond : IF !index.html AND !app/facebookapp/{[a-zA-Z0-9-/]}.html
RewriteRule : .....
My code is
RewriteCond %{REQUEST_URI} !index\.html
RewriteCond %{REQUEST_URI} app/facebookapp/^([a-zA-Z0-9-/]+).html$
RewriteRule ......
its not working
And one more question
if the request url is header.html
RewriteCond %{REQUEST_URI} header.html$
RewriteRule ^([a-zA-Z0-9-/]+).html$ position.php?position=$1 [L]
$l will return header.
if the request url is app/facebookapp/header.html
we write the same above condition $l will return app/facebookapp/header.my question is how to get only the filename ??
thanks

Part 1:
Looks to me like you've got the ^ character in the wrong place. In regex syntax, that denotes the beginning of the text but you've got some text before it. You could put it at the front of the thing, but I think in this case you could just get rid of it. Try this:
RewriteCond %{REQUEST_URI} !index\.html
RewriteCond %{REQUEST_URI} app/facebookapp/([a-zA-Z0-9-/]+).html$
RewriteRule ......
Part 2:
You'll need two matching groups, and to only use the second one in the right-hand side. The first one includes / as a valid character, and the second one does not. Let me know if this works better:
RewriteCond %{REQUEST_URI} header.html$
RewriteRule ^([a-zA-Z0-9-/]+)/?([a-zA-Z0-9-]+).html$ position.php?position=$2 [L]

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.

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.

Htaccess redirect depending on the value of variable

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]