.htaccess - Internal friendly URL rewrite for one parameter only - apache

I am having trouble using .htaccess to internally rewrite (that is, use the requested URL to form an internal request to then provide that to the client, who still only sees the original requested URL) a URL where only one parameter is prettified, and the rest of the request parameters are still appended. Other posts on stack either concern just one relevant parameter, or wish to redirect every parameter.
That is,
https://new.mysite.com/overhoringen/open/7 should internally request https://new.mysite.com/overhoringen/open?testId=7
https://new.mysite.com/overhoringen/open/9?other=param&more=param should internally request
https://new.mysite.com/overhoringen/open?testId=9&other=param&more=param
I can do this for the first bullet, a single parameter rewrite;
RewriteEngine on
RewriteBase /
#Prettify test
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?overhoringen/open/([^/]+)/?$ /overhoringen/open.php?testId=$1 [L]
However, I am unsure how to capture the request query at the end and then append it to the internal redirect (if present at all), without the ? still in front (to avoid open?testId=9?other=param&more=param), etc.
Help with this would just be really cool. :]

Change this line:
RewriteRule ^/?overhoringen/open/([^/]+)/?$ /overhoringen/open.php?testId=$1 [L]
to:
RewriteRule ^/?overhoringen/open/([^/]+)/?$ /overhoringen/open.php?testId=$1 [L,QSA]
Adding QSA should append the additional query string to the new url.

Related

.htaccess RewriteRule from long url to show short url

Im trying to rewrite url from long to short but cant wrap my head around this.
My survey rewrite works wonderfully but after completing my survet php redirects to www.example.com/survey_thank_you.php?survey_id=1
but I would like to show url like www.example.com/thank_you
Im not even sure if this is possible.
Im new with .htaccess and i have tried almost everthing
.htaccess
Options +FollowSymLinks
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^survey_thank_you.php?survey_name=([0-9a-zA-Z]+)/?$ Thank_you [L,NC,QSA]
RewriteRule ^([0-9a-zA-Z]+)/?$ survey_form.php?survey_name=$1 [L,NC,QSA] #works like charm.
Any help or directions will be highly appreciated.
Solution:
Options +FollowSymLinks
Options -MultiViews
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^survey_id=([0-9a-zA-Z]+)/?$
RewriteRule ^survey_thank_you\.php$ /%1/thank_you [R,L,QSD]
RewriteRule ^([0-9a-zA-Z]+)/thank_you$ survey_thank_you.php?survey_id=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([0-9a-zA-Z]+)/?$ survey_form.php?survey_name=$1 [L,NC,QSA]
but after completing my survet php redirects to www.example.com/survey_thank_you.php?survey_id=1
You need to "correct" the URL that PHP is redirecting you to after the survey. If the desired URL is /thank_you (or /Thank_you?) then PHP should be redirecting to that URL.
You then use mod_rewrite in .htaccess to internally rewrite /thank_you back into the URL that your application understands. ie. /survey_thank_you.php?survey_id=1. However, therein lies another problem, where does the 1 (survey_id) come from in the query string? Presumably you don't want to hardcode this? So this would need to passed in the requested URL. eg. /1/thank_you or perhaps /thank_you/1?
However, is this really necessary? The resulting "thank you" page is not a page that should be indexed or a page that is normally navigated to by the user, so implementing a user-friendly URL here doesn't seem to be a worthwhile exercise?
RewriteRule ^survey_thank_you.php?survey_name=([0-9a-zA-Z]+)/?$ Thank_you [L,NC,QSA]
RewriteRule ^([0-9a-zA-Z]+)/?$ survey_form.php?survey_name=$1 [L,NC,QSA] #works like charm.
You are using a survey_name URL parameter (referencing an alphanumeric value) in your directives, but a survey_id ("numeric"?) URL parameter in your earlier example? So, which is it? Or are these rules unrelated?
You state that the second rule "works like charm", but how? What URL are you requesting? That would seem to rewrite /Thank_you to survey_form.php?survey_name=Thank_you - but that does not look correct?
As mentioned in comments, the RewriteRule pattern matches against the URL-path only. To match against the query string you need an additional condition that matches against the QUERY_STRING server variable. This would also need to be an external 3xx redirect, not an internal rewrite (in order to change the URL that the user sees). Therein lies another problem... if you don't change the URL that your PHP script is redirecting to then users will experience two redirects after submitting the form.
You also need to be careful to avoid a redirect loop, since you are internally rewriting the request in the opposite direction. You need to prevent the redirect being triggered after the request is rewritten. ie. Only redirect direct requests from the user should be redirected.
So, to answer your specific question, it should be rewritten something like this instead:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^survey_name=[0-9a-zA-Z]+/?$
RewriteRule ^survey_thank_you\.php$ /Thank_you [QSD,R,L]
The check against the REDIRECT_STATUS environment variable ensures that only direct requests are processed, not internally rewritten requests by the later rewrite. REDIRECT_STATUS is empty on the initial request and set to the string 200 (as in 200 OK status) after the first successful rewrite.
The QSD flag (Apache 2.4) is necessary to discard the original query string from the redirect response.
So the above would redirect /survey_thank_you.php?survey_name=<something> to /Thank_you.
But this is losing the "survey_name" (or survey_id?), so should perhaps be more like the following, in order to preserve the "survey_name":
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^survey_name=([0-9a-zA-Z]+)/?$
RewriteRule ^survey_thank_you\.php$ /%1/Thank_you [QSD,R,L]
Where %1 is a backreference to the value of the survey_name URL parameter captured in the preceding CondPattern.
However, you would then need to modify your rewrite that turns this back into an understandable URL.
(But you should probably not be doing this in the first place without first changing the actual URLs in the application.)

.htaccess - silently rewrite/redirect everything to internal subfolder

Let's say I have thiswww.example.com site structure:
/srv/http/
/srv/http/site/index.php
/srv/http/site/stuff.php
I want the following rewrites/redirects to happen:
www.example.com/index.php -> redirects to -> www.example.com/site/index.php -> but the user sees -> www.example.com/index.php
www.example.com/stuff.php -> redirects to -> www.example.com/site/stuff.php -> but the user sees -> www.example.com/stuff.php
In general, everything after www.example.com/ redirects to www.example.com/site/. But the user sees the original URL in the browser.
I've looked around on the internet but haven't managed to figure out what to use in this particular situation.
I tried rewriting everything:
RewriteEngine On
RewriteRule ^$ /site [L]
but index.php disappears and www.example.com/site/ is shown to the user.
How can I use .htaccess to solve this problem?
You need to capture the url request incoming into the server, like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule ^(.*)$ /site/$1 [L,QSA]
The QSA is (eventually) to also append the query string to the rewritten url
Same idea as #guido suggested, but a bit shortened using negative lookahead
RewriteEngine On
RewriteRule ^(?!site/)(.*)$ site/$1 [L]
Note: I am not using QSA flag as we are not adding additional parameters to the query string for the replacement URL. By default, Apache will pass the original query string along with the replacement URL.
http://www.example.com/index.php?one=1&two=2
will be internally rewritten as
http://www.example.com/site/index.php?one=1&two=2
If you really want add a special parameter (ex: mode=rewrite) in the query string for every rewrite, then you can use the QSA Query String Append flag
RewriteEngine On
RewriteRule ^(?!site/)(.*)$ site/$1?mode=rewrite [L,QSA]
Then this will combine mode=rewrite with original query string
http://www.example.com/index.php?one=1&two=2
to
http://www.example.com/site/index.php?mode=rewrite&one=1&two=2
RewriteEngine On
RewriteRule ^(.*)$ site/index.php?var=$1 [L]
With this rule, i'm passing all requests to site/index.php, so you could get the requested uri via $_GET['var'], and then you'll make the index.php serve the requested url behind the scene without the url changing in the user's browser. Ciao.

Add parameter using htaccess on condition

This will be a simple for those familiar with Apache rules.
Situation
Using Alipay for a payment platform, the return URL cannot feature any of your own URL parameters (be it GET or POST). However, I am using Joomla and specifically Akeeba subscriptions. This component expects a parameter in the URL in accordance with the payment platform in question.
I want to detect (through one of Alipay's URL parameters) when a return page is hit and add the extra parameter.
Example (domain and page redacted)
http://...?
currency=HKD&
total_fee=2.00&
out_trade_no=211&
trade_no=2014040100276615&
trade_status=TRADE_FINISHED
Desired outcome
http://...?
currency=HKD&
total_fee=2.00&
out_trade_no=211&
trade_no=2014040100276615&
trade_status=TRADE_FINISHED&
paymentmethod=alipay
The simple addition of a &paymentmethod=alipay
Problem
I can't seem to get Apache to pick up the rule; here are a couple of attempts so far. Please note, I definitely can use .htaccess and don't need to change RewriteBase.
-- Attempt 1 --
RewriteCond %{QUERY_STRING} out_trade_no=
RewriteRule ^out_trade_no paymentmethod=alipay&out_trade_no [R,L,QSA]
-- Attempt 2 --
RewriteCond %{QUERY_STRING} (^|&)out_trade_no=(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}&paymentmethod=alipay [L,R=301,QSA]
Progress
Combining the two, I have made progress but, now seem to have the Rewrite part spamming "paymentmethod=alipay" which seems to cause an error.
RewriteCond %{QUERY_STRING} out_trade_no=
RewriteCond %{QUERY_STRING} !paymentmethod=
RewriteRule ^ %{REQUEST_URI}&paymentmethod=alipay [R,L]
Now getting a redirect chain until it automatically stops at a redirect limit
If you are just trying to match a query string from that URL with that rewritecond you need to match the first one(currency). Which is the easiest.
Try this. It will send all the parameters you want.
RewriteCond %{QUERY_STRING} ^\bcurrency=
RewriteRule ^(.*)$ /$1?paymentmethod=alipay [R,QSA,L]

Simple mod_rewrite for search querystring

I am trying to rewrite
/search?keyword=foobar
to
/search/foobar
without much success.
I currently have the following which seem to produce a 404:
RewriteCond %{QUERY_STRING} ^keyword=(.*)$ [NC]
RewriteRule .* /search/%1? [L,R=301]
Unless you have a resource at /search/foobar then of course you're going to get a 404. Two entirely different things are happening here. The server has a physical resource that gets served (or a script that runs) that apache knows about. If apache sees /search/foobar, it is going to look for a directory called "search" and either a directory or a file called "foobar". If it sees neither, it's going to return a 404. The other part of what's happening is the browser, completely separate from apache, sees a URL (e.g. /search/foobar) and does what it needs to do in order to request for the resource. It talks to the webserver and asks for /search/foobar.
When the request comes in, it's up to the URL-file processing pipeline to turn that into a file which points to where the resource is. If mod_rewrite takes the URL and rewrites it to /blah/blah/blah, there better be a directory called /blah/blah and a file in there called blah or else it's going to 404.
Your rules are saying, if an incoming request is for anything with the query string ?keyword=(something), then redirect the browser to /search/(something). The browser sees this, and does what it's supposed to do; it sends another request for /search/(something). Apache's going to see this and wonder what the request is all about, not knowing what the request is for, and return 404.
What you probably want is to first, handle the /search/(something) URI's
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?search/(.*)$ /search?keyword=$1 [L,QSA]
So when a request comes in as /search/foobar, the rewrite engine internally rewrites it to something apache can understand, /search?keyword=foobar. This internal rewrite happens entirely on the server, the browser is ignorant of it.
Now, when a form is submitted as a GET method, you'll end up with a ?keyword=(something) in the URL, and it looks like you're trying to get rid of that. So apache gets the query string, and there must be something to redirect the browser to the nicer looking URL, at which point the browser does its thing, submits a brand new request, which gets internally rewritten by the above rule back to what it should be.
RewriteCond %{THE_REQUEST} \?keyword=([^\ &]+)&?([^\ ]*)
RewriteRule ^ /search/%1?%2 [L,R=301]
I sorted it out with the following:
RewriteRule search/(.*)$ /search?keyword=$1 [L]
RewriteCond %{THE_REQUEST} \?keyword=([^\ &]+)&?([^\ ]*)
RewriteRule ^ /search/%1?%2 [L,R=301]
but not quite. Having issues where there are multiple querystrings or some other URLs containing search/ in the URL, eg. /search/css/foobar.css?version=152

What's going on with my mod_rewrite?

I have a simple mod_rewrite system set up on my site which basically converts
http://site.com/file -> http://site.com/file.php
Here's the .htaccess file
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.site.com
RewriteRule ^(.*)$ http://site.com/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)/?$ http://site.com/$1.php [L]
This was working for a long time and then a couple of days ago I realized that while the RewriteRule was working, it was actually changing my URL in the status bar.
For instance, it would redirect /photos to /photos.php, but it would also change the URL to show the .php. This has never happened before and I'm not sure what happened to trigger the change.
Any ideas?
The first rewrite rule needs the [L] flag. From the mod_rewrite documentation for the [R] flag:
You will almost always want to use [R] in conjunction with [L] (that is, use [R,L]) because on its own, the [R] flag prepends http://thishost[:thisport] to the URI, but then passes this on to the next rule in the ruleset, which can often result in 'Invalid URI in request' warnings.
In this case, you don't get a warning, but appending the ".php" extension happens before issuing the redirect rather than when the second, redirected request comes in.
Also, remove the scheme and domain name from the substitution in the second rewrite rule. A full URL can cause an implicit redirect. From the documentation for RewriteRule:
The Substitution of a
rewrite rule is the string that replaces the original URL-path that
was matched by Pattern. The Substitution may
be a:
[...]
Absolute URL
If an absolute URL is specified,
mod_rewrite checks to see whether the
hostname matches the current host. If it does, the scheme and
hostname are stripped out and the resulting path is treated as
a URL-path. Otherwise, an external redirect is performed for
the given URL. To force an external redirect back to the
current host, see the [R] flag below.