.htaccess rewriting a URL which is passing 2 parameters - php

assume that i'm having a URL like this
testingQuiz.com/my_test_link.php?id=4&name=test
Now I need to rewrite the above URL like this,
testingQuiz.com/4/test.html
My question is, how can I achieve that by .htaccess URL rewriting?

Something like the following perhaps should work, this is not fully tested btw but preliminary tests show it does as expected - though perhaps the second style redirect could be refined.
/* turn on url rewriting */
RewriteEngine On
/* set the level for the rewriting, in this case the document root */
RewriteBase /
/* match 2 parameters in querystring - the first is numeric and the second is alphanumeric with certain other common charachters */
RewriteRule ^([0-9]+)/([a-zA-Z0-9_-]+)\.html$ my_test_link.php?id=$1&name=$2 [NC,L]
That should allow you to write your urls / links in the form http://www.example.com/23/skidoo.html and for the $_GET variables to be interpreted as:
$_GET['id'] => 23, $_GET['name'] => skidoo
If you need to automagically redirect the user from your original style querystring to the new, nicer style url you could try adding the following after the rule above:
RewriteCond %{THE_REQUEST} /(?:my_test_link\.php)?\?id=([^&\s]+)&name=([^&\s]+) [NC]
RewriteRule ^ %1/%2\.html? [R=302,L,NE]
That way, if a user enters the url http://www.example.com?id=23&name=skidoo it will redirect to http://www.example.com/23/skidoo.html

Related

htaccess - Redirect to the same location irrespective of presence of trailing slash

I want to redirect the following sets of links:
a/b/c or a/b/c/ to a.php?b=c
x/y1/z1/y2/z2 or x/y1/z1/y2/z2/ to x.php?y1=z1&y2=z2
using htaccess and mod rewrite in a standardized general format associating the appropriate PHP get tags and values to the corresponding SEO-friendly link. How do I do so?
I've tried tinkering around with RewriteCond and REQUEST_FILENAME but just cannot seem to get it to work.
Something like this might help:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/? /$1.php?$2=$3 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/? /$1.php?$2=$3&$4=$5

URL structure change using .htaccess rule

I have no knowledge in regex and never done regex redirections.
htaccess
but... I am looking for a way to redirect this URL structure:
http://www.example.com/mypage/param1/
to this URL structure
http://www.example.com/mypage/?key1=param1
and, if possible, only if param1 contains # or %40
I saw this answer:
How to change URL structure with htaccess?
but that's the reveresed direction for my question
RewriteEngine on
RewriteCond $1 #
RewriteRule ^mypage/((?!index).+)$ /mypage/?key=$1 [NC,L,R]
Remove the R flag if you dont want the url to change.
This will redirect an url of the form :
/mypage/.*#.*
to
/mypage/?key=.*#.*

mod_rewrite: set custom header through .htaccess

I'have this rewrite condition that redirect all the request of non-existing files to the app.php in parent directory.
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^(.*)$ %2../app.php [L]
In the last line (rewriterule) the value of $1 is the path relative to the dir where .htacces is, in order to make it portable ( found here: http://linlog.skepticats.com/entries/2014/08/Using_RewriteBase_without_knowing_it.php ).
That is : calling http://localhost/myapp/public/test123/r.txt I will get test123/r.txt in $1 variable (considering that .htaccess is in public/).
I would like to pass the value of $1 to the app.php, a common solution would be to append it as a query string:
RewriteRule ^(.*)$ %2../app.php?__path=$1 [L]
In this way I have __path as a GET variable (PHP: $_REQUEST["__path"]) , but how it works with other kind of requests like POST (query string should not be there) ?
A more clean solution would be to put $1 in a HTTP custom header but, how can I set a custom header, say MYAPP_PATH to $1 value through .htacess ?
To start with your comment:
I've noticed that the trick ?__path=$1 overrides all the querystring that I pass in the original URL.
You forgot to set the QSA flag.
Second to the core question:
In this way I have __path as a GET variable (PHP: $_REQUEST["__path"]) , but how it works with other kind of requests like POST (query string should not be there) ?
This just works fine. GET-parameters are just popularly called that, they're actually query string parameters and can be passed with any HTTP verb, from GET and POST to PUT and DELETE. So if you POST to a URL with query string parameters you can still read them from $_GET just fine.
Final point, as the above solves all your issues: please do not use $_REQUEST. It's really REALLY bad practice, and may lead to obscure security and stability issues. Just use $_POST, $_GET et al instead.

Redirect URI into subpage.php and read is as parameter by .htaccess

I would like to redirect URI into subpage.php and read is as parameter (by $_GET method).
Example 1:
http://homepage.com/wap/show_counties.php?id_county=51&char_county=&lang=en
into
http://homepage.com/controller.php?url=wap/show_counties.php?id_county=51&char_county=&lang=en (??? - can I pass url parameter like this?)
Example 2:
http://homepage.com/accommodation/hostel-star-bratislava
into
http://homepage.com/controller.php?url=accommodation/hostel-star-bratislava
Example 3:
http://homepage.com/en/search-in-page-12.html?ordering=newest&searchphrase=any&searchword=do%25252525252525252525252B700%252525252525
into
http://homepage.com/controller.php?url=en/search-in-page-12.html?ordering=newest&searchphrase=any&searchword=do%25252525252525252525252B700%252525252525 (??? - can I pass url parameter like this?)
EDIT: I would like to make url redirection by .htaccess
Could you help me please?
Regards
Jan Zitniak
You can do this with mod_rewrite. What you want to do can be described like so:
The request uri should be passed to a specific file in a get variable
If there is an original query string, it should be retained
You can do this with the QSA (query string append) flag in mod_rewrite. In addition to that, in the example below I add a condition that checks if the file that is requested does not exist. This prevents the rule from matching itself (controller.php exists) and prevents the rule from working on files that are typically not handled by a controller (js/css/images). Add the following to the beginning of the .htaccess in your www-root:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /controller.php?url=$1 [QSA,L]
See the documentation for mod_rewrite in general and the document about flags specific for more information.

Build PHP page based on url

If you have a url such as the one as follows:
http://www.example.com/400x200
is it possible to create a page which echos out 400x200 when the user visits that url using php?
I know how to echo the path - that is easy enough (ltrim($_SERVER['PATH_INFO'], '/')), but do not know how to create the page dynamically.
Any help would be much appreciated, thanks in advance
The request URI (/400x200) is stored in the server superglobal: $_SERVER["REQUEST_URI"].
You need to take that and route the URI accordingly. The simplest possible scenario: in your index.php, place this code:
$uri = trim($_SERVER["REQUEST_URI"],"/");
if (preg_match("/\d+x\d/")) {
list($width,$height) = explode("x",$uri);
// some logic with the above vars, e.g. include a view script
}
What this does is check whether the URI has the format {number}x{number}, extracts both numbers and stores them in the variables $width and $height. You can then do whatever you like with the variables.
In order to make the request always point to the file containing this code, edit your .htaccess and put in something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
(the .htaccess code is copied from the default Zend Framework project, in case anyone asks).
You may want to look at Apache Rewrites for rewriting your URL:
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
Do not know what do You mean by creating the page dynamically but I guess that using of mod_rewrite is what You need.
In Your .htaccess file You have to create some rules that will rewrite the URL to something distinguishable by Your PHP script - like from URL
http://www.example.com/400x200
to get
http://www.example.com/index.php?param=400x200
And then You can in Your index.php script do echo $_GET['param'];...
Google something about PHP and mod_rewrite: http://www.google.com/#q=PHP+mod_rewrite
Assuming you're using Apache, this can be done using something called URL rewriting. Create a file called .htaccess in your document root, and add this:
# Turn URL rewriting on
RewriteEngine on
Options +FollowSymlinks
# Rewrite rule
RewriteRule ^(\d+x\d+)/?$ index.php?dimensions=$1 [L]
The first two lines turn the rewrite engine on, and the third line defines a RewriteRule. The first part ^(\d+x\d+)/?$ is a regular expression that the part of the URL after the domain will be matched against.
The second part index.php?dimensions=$1 is the URI that will be rewritten to. The client doesn't see this, but PHP will.
If I do a print_r($_GET) in index.php with the URL http://localhost/400x300, I get this:
Array ( [dimensions] => 400x300 )
This is from the standard $_GET superglobal array in PHP and can be used as normal. URL rewriting leaves the URL as it is in the browser, yet allows you to turn it into one usable by PHP with a query string.
To make your script a bit easier to use, you could split the expression up to get separate X and Y values:
RewriteRule ^(\d+)x(\d+)/?$ index.php?x=$1&y=$2 [L]
Which will give an array like this:
Array ( [x] => 400, [y] => 300 )
Make it a GET variable like
http://www.example.com?size=400x200
Then you can retrieve the String with
$size = $_GET['size'];
What I'd recommend you to do is to get the values based on split or explode()
$lw = $size.explode('x',$size);
$length = $lw[0];
$width = $lw[1];
//Manipulate the values accordingly like
echo $length.'x'.$width;

Categories