Rewrite URL on Site using Nginx - php

I currently have my URL like so:
mysite.com/test/?country=uk&number=1234&search=British+Telecom&limit=8
which I want to be like this:
mysite.com/test/uk/1234/British-Telecom/8
I am familiar with Apache mod_rewrite to some extent but never come across Nginx version. Anyone have an idea?
Something else I found is that the site is Wordpress and normal pages work fine with the url being rewritten, but my code is in a separate non-wp folder. How come WP's .htaccess seems to work when Apache is not running?
Another complication is that I have Varnish cache working - could this interfere?

you must capture get variables with ([^/]*) regex and then use it with $1,$2 ...
use a htaccess like this :
RewriteEngine on
RewriteRule ^test/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ ./yourScript.php?country=$1&number=$2&search=$3&limit=$4 [L]
change yourScript.php with your script name .

Related

CakePHP routing without trailing path info

I have a simple CakePHP which I want to use without URL rewriting. The site which works fine locally but not on my hosting server. My sysadmin has advised me that:
The PHP wrapper doesn't accept trailing path-info like script.php/additional/path/info
CakePHP seems to expect this by default. How do I get around this limitation? I guess I need to pass the path information via the query string instead, so instead of URLs like this:
http://example.com/index.php/controller/action
I probably will need something like this:
http://example.com/index.php?path=controller/action
I'm sure this should be doable by changing the routing scheme somehow, but how? I have tried reading the docs, and digging into the framework source code is kind of daunting... Thanks!
Your simple solution is how to remove trailing slash from URL using .htaccess. This may be done by the following rewrite rule:
RewriteRule ^(.*)/$ $1 [R=301,L]
This should be placed in the .htaccess file where your application's index.php.

Mapping URL into custom files

I want to map URL in my localhost XAMPP into custom files.
For example:
localhost/index.php --> d:\xampp\htdocs\index.php (default)
localhost/normal/data.php --> d:\xampp\htdocs\normal\data.php (default)
localhost/view/userinfo.php --> d:\xampp\htdocs\view.php?p=userinfo (custom)
localhost/view/welcome.php --> d:\xampp\htdocs\view.php?p=welcome (custom)
So, basically, all URL that goes into inside view path will be mapped to view.php files with the filename.php (minus the .php) as its query parameter. There's actually no physical folder view, and no physical files userinfo.php and welcome.php inside the folder.
The reason that I need to do this is that so I can pass all the pages that viewing data into an "application frame" that will wrap the page with header, menu, and footer, and I don't need to give header, menu, and footer call in each page. I might have the actual files userinfo.php that I can $include_once, or I might not (I can just generate it from within the view.php), but hey, that's one of the power of this kind of framework, right? And, if someday I need to change this structure, I can change it from just within one file (view.php), not all.
Can I do this in PHP and XAMPP? How? I've noticed that some website seems to used this practice (URL which have no actual files or even path at all), but when I try to read tutorial for it, I got confused.
URL mapping in PHP?
The accepted answer listed 3 links to learn about URL rewriting. Mostly they're written for Apache in Linux, and mostly they pull all the possible scenario and configuration that I got confused which one I really need with all those long documents and technical jargon. I need just the practical step of my specific problem, and then, I will be able to start from there to explore myself if I have more advanced needs. Please help.
if you do want to go down the mod rewrite route adding the following to an .htaccess file in the site root should do it. You will need to make sure mod rewrite is on for XAMPP and I can't help you there I'm afraid. As you can see it rewrites the url, not the windows filename - so it would work on any OS.
The ([a-z]*) means it will take any filename.php with lowercase letters and redirect to /view.php?p=$1 where the $1 will be replaced by filename.
the [L,R] (L means last rule so stop processing if any more are reached, and the R means redirect (it will change the url in the browser). Use P instead to reverse Proxy (the user will still see the url they requested but the server will serve the correct file) - This will require mod_proxy as well.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^view/
RewriteRule ^view/([a-z]*).php$ /view.php?p=$1 [L,R]
</IfModule>
XAMPP uses apache so the rewrites would work the same in Windows as they do in Linux. You could place a .htaccess in the site root directory with some rewrite rules.
However, using PHP
in d:\xampp\htdocs\view\userinfo.php you could include the line
<?php
header('Location: http://localhost/view.php?p=userinfo');
?>
But this must be before any thing is echoed to the screen (even whitespace).
You can use the Apache module mod_rewrite to edit requests before they hit PHP. You want to put something like the following in a .htaccess file in your htdocs directory.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^view/
RewriteRule ^view/(.*)\.php.*$ view.php?p=$1 [L,QSA]
QSA means Query String Append. This means that if there are any GET parameters set on the original request they will be appended to the end of the new request too.
Note that this assumes that Apache is configured with AllowOverride enabled and the mod_rewrite module loaded.

.htaccess direct domain to subdirectory

I am trying to redirect www.example.com to /example/ on my webserver, but it appears to only be redirecting the index.php page. An additional issue is the main file displays as http://tunedu.com/tunedu/ when I would like it to display as tunedu.com
Live example:
This page works: http://tunedu.com/tunedu/
This page doesn't work: http://tunedu.com/school.php?id=75
Any regex changes I do try end up just breaking everything. The .htacess code is this:
RewriteEngine on
RewriteRule ^$ /tunedu/ [L]
Thanks.
The proper way to do such a thing is by setting VirtualHosts on your webserver (either Apache, nginx or another...). Using htaccess for this seems quite painful.
Assuming you're using Apache, here's a useful link: http://httpd.apache.org/docs/current/vhosts/examples.html
I'm not sure why you are not using VirtualHost to set that up.
But in case you want to go the mod_rewrite way here is an useful link:
http://httpd.apache.org/docs/2.2/rewrite/vhosts.html
I hope that helps.

How to make a virtual Directory?

http://wiki.answers.com/Q/What_do_the_abbreviations_mean_on_an_ultrasound_scan
In above url, it looks like "What_do_the_abbreviations_mean_on_an_ultrasound_scan" is a directory!! but i think its a virtual directory !!
I want to know about this stuff !! how to do these things and what they call ???
They are most probably using mod_rewrite to rewrite the url. A good resource is the tag wiki for mod_rewrite here on StackOverflow.
The rules for mod_rewrite is stored in the .htaccess file, and looks something like this:
RewriteRule ^/Q/(.*?)$ answers.php?slug=$1
This would rewrite all requests whitch match a url starting with /Q/ to answers.php, and provide whatever is after (in this case What_do_the_abbreviations_mean_on_an_ultrasound_scan) as a GET parameter. This would be accessible in the script as $_GET['slug'] if they were running PHP.

Domain/URL Masking

I have a website that passes some GET variables to different pages in PHP. My issue is that now I have a url with variables i.e. index.php?category=categoryname and that's not very memorable.
Is there any way I can change the URL to something like /categoryname instead without duplicating the page and storing in folders? But also allow users to type in /categoryname and be redirected to the correct page?
.htaccess Apache mod_rewrite, almost every professional dynamic website uses this method (like stackoverflow).
The method is fully explained in this article far better then I could ever explain it in this answer box.
You should look into writing some apache Mod_Rewrite rules in a .htaccess file.
The solution is discussed here:
this is done by the rewrite module of apache and this handles regular
expressions. You have to put a rule
like this in your .htaccess file on
the root of your website:
RewriteRule ^cat/([0-9]+)$
/index.php?category=$1
^ means the start of the url after
www.example.com/ $ means the end of
the page.
www.example.com/cat/123
will be converted by the server to:
www.example.com/index.php?category=123
In PHP you use the normal $_GET['id']
variable. The rewrite module must be
enabled by apache... This is mostly
used to make the url format
independent of the serverside
scripting language so the .php in the
url is not logical. Thats why i
changed it to product/ . The .htaccess
starts with
RewriteEngine On Options
+FollowSymLinks RewriteBase / Here all the rewrite rules.. ...

Categories