Nginx - URL rewrite rules - mod-rewrite

I have for example this URL:
www.example.com/folder1/folder2/edit.php?username=nickname
Actually I have this rewrite rules:
location / {
root /var/www;
index index.php index.html index.htm;
# First rewrite rule output: www.example.com/nickname
rewrite ^/([A-Za-z0-9_]+)$ /folder1/folder2/user.php?username=$1;
# Second rewrite rule output: www.example.com/nickname/edit
rewrite ^/([A-Za-z]+)/edit$ /folder1/folder2/edit.php?username=$1;
}
However, that works fine.
But I need to rewrite my URL like this:
www.example.com/nickname/edit/info
It has a couple of parameters:
www.example.com/folder1/folder2/edit.php?username=nickname&info=basic
I tried everything but with no success:
location / {
root /var/www;
index index.php index.html index.htm;
# First rewrite rule output: www.example.com/nickname
rewrite ^/([A-Za-z0-9_]+)$ /folder1/folder2/user.php?username=$1;
# Second rewrite rule output: www.example.com/nickname/edit
rewrite ^/([A-Za-z]+)/edit$ /folder1/folder2/edit.php?username=$1;
# Here where I'm stuck
# www.example.com/nickname/edit/info
rewrite ^/edit/([A-Za-z]+)/info$ /folder1/folder2/edit.php?user=$1&info=$2;
}

Your last attempt seems to have mixed up the location of edit. Also, nothing is passed to info as there is only one capturing group, perhaps
rewrite ^/([A-Za-z]+)/edit/([A-Za-z]+)$ /folder1/folder2/edit.php?username=$1&info=$2;
will work instead?

Related

Lighttpd rewrite-if-not-file cannot process *php and *html in one rule

To enable a shorten-url service, I had to add a rewrite rule to my
lighttpd.conf. This seems to be important, otherwise shortlinks like
http://example.com/Xf6Y would return a 404 error. This is because Xf6Y
is not a file but a shortlink hash.
So I added these lines to my lighttpd.conf:
url.rewrite-if-not-file = (
"^/(.*)$" => "/index.php?fwd=$1"
)
This works excellently, except for websites that contain a index.html
instead of an index.php.
I tried to duplicate the rewrite rule, like url.rewrite-if-not-file = (
"^/(.)(\?.)?$" => "/index.php?fwd=$1",
"^/(.)(\?.)?$" => "/index.html?fwd=$1"
)
This resulted in a "duplicate rule" error, so lighttpd is
not able to restart.
Does anyone know how to write a rewrite rule that will redirect index.php and index.html?

Redirect example.com/?p=25 to example.com/25 in CodeIgniter

I'm using CodeIgniter and I want to redirect links like this:
example.com/?p=25
to this:
example.com/25
How can this be achieved?
http://www.askaboutphp.com/58/codeigniter-mixing-segment-based-url-with-querystrings.html
or
to create url like this : http://yyyy.com/article/finishing-dan-snapshop-salkulator
add this code in routes.php
$route['article/(:any)'] = "article/readmore/$1";
description :
1. article : class name
2. readmore : method from class article
3. $1 : get value from uri segment 2 value
its .htaccess rewrite rule.make sure u have activated mod_rewrite .then put this line into website application root .htaccess file
RewriteRule ^/([0-9]+)/?$ p=$1 [NC,L] # Handle product
requests

Nginx rewrite rule with proxy pass

I'm trying to implement nginx rewrite rules for the following situation
Request:
http://192.168.64.76/Shep.ElicenseWeb/Public/OutputDocuments.ashx?uinz=12009718&iinbin=860610350635
Should be redirected to:
http://localhost:82/Public/OutputDocuments.ashx?uinz=12009718&iinbin=860610350635
I tried this with no luck:
location /Shep.ElicenseWeb/ {
rewrite ^/Shep.ElicenseWeb/ /$1 last;
proxy_pass http://localhost:82;
}
What is the correct way to perform such a rewrite for nginx ?
Your rewrite statement is wrong.
The $1 on the right refers to a group (indicated by paratheses) in the matching section.
Try:
rewrite ^/Shep.ElicenseWeb/(.*) /$1 break;
You're missing a trailing slash:
location /Shep.ElicenseWeb/ {
proxy_pass http://localhost:82/;
}
This will work without a rewrite.

mod_rewrite and mod_dir collision

I'm edditing my htaccess to internally redirects
just about any URL to a php page handler:
RewriteRule ^images\/ - [L,NS]
RewriteRule ^docs\/ - [L,NS]
RewriteRule ^([A-Za-z0-9\_\-]+)\/?$ pages/pagehandler.php?page=$1 [L,QSA,NS]
this works fine accept that directories that are enteres in the address
bar with no trailing slash for some reason get duplicate query strings,
and for some reason the address bar of the browser changes
for example, if I type the URL:
localhost/movies
if there's a directory called movies in the site root
than the address changes to:
localhost/movies/?page=movies
I guess this is some collision with mod_dir but I don't know
how to overcome it, if I use:
<IfModule mod_dir.c>
DirectorySlash Off
</IfModule>
Than it works, but I don't want this, I think for some reason the url
is rewritten than mod dir changes it and than it is rewritten again thus
making duplicate query strings,
Any Ideas?
EDIT: I Add a relevant part of the Rewritelog, this is all from one request:
strip per-dir prefix: /opt/lampp/htdocs/movies -> movies
applying pattern '^images\/' to uri 'movies'
strip per-dir prefix: /opt/lampp/htdocs/movies -> movies
applying pattern '^docs\/' to uri 'movies'
strip per-dir prefix: /opt/lampp/htdocs/movies -> movies
applying pattern '^pages\/' to uri 'movies'
strip per-dir prefix: /opt/lampp/htdocs/movies -> movies
applying pattern '^([A-Za-z0-9\_\-]+)\/?$' to uri 'movies'
rewrite 'movies' -> 'pages/pagehandler.php?page=movies'
split uri=pages/pagehandler.php?page=movies -> uri=pages/pagehandler.php, args=page=movies
add per-dir prefix: pages/pagehandler.php -> /opt/lampp/htdocs/pages/pagehandler.php
trying to replace prefix /opt/lampp/htdocs/ with /
strip matching prefix: /opt/lampp/htdocs/pages/pagehandler.php -> pages/pagehandler.php
add subst prefix: pages/pagehandler.php -> /pages/pagehandler.php
internal redirect with /pages/pagehandler.php [INTERNAL REDIRECT]
strip per-dir prefix: /opt/lampp/htdocs/movies/ -> movies/
applying pattern '^images\/' to uri 'movies/'
strip per-dir prefix: /opt/lampp/htdocs/movies/ -> movies/
applying pattern '^docs\/' to uri 'movies/'
strip per-dir prefix: /opt/lampp/htdocs/movies/ -> movies/
applying pattern '^pages\/' to uri 'movies/'
strip per-dir prefix: /opt/lampp/htdocs/movies/ -> movies/
applying pattern '^([A-Za-z0-9\_\-]+)\/?$' to uri 'movies/'
rewrite 'movies/' -> 'pages/pagehandler.php?page=movies'
split uri=pages/pagehandler.php?page=movies -> uri=pages/pagehandler.php, args=page=movies&page=movies
add per-dir prefix: pages/pagehandler.php -> /opt/lampp/htdocs/pages/pagehandler.php
trying to replace prefix /opt/lampp/htdocs/ with /
strip matching prefix: /opt/lampp/htdocs/pages/pagehandler.php -> pages/pagehandler.php
add subst prefix: pages/pagehandler.php -> /pages/pagehandler.php
internal redirect with /pages/pagehandler.php [INTERNAL REDIRECT]
Also the relevant part from the access log:
"GET /movies HTTP/1.1" 301
"GET /movies/?page=movies HTTP/1.1" 200
Can you change your rewrite rule to:
RewriteRule ^([A-Za-z0-9\_\-]+)/?$ /pages/pagehandler.php?page=$1 [L,QSA,NS]
Note the / before pages.
For anyone having this problem, what I did ultimately in disable the Directory Slash:
<IfModule mod_dir.c>
DirectorySlash Off
</IfModule>
And I used a RewriteRule to 301 Redirect all directory requests than do not end with a /
I guess this is some sort of collision between mod_rewrite and mod_dir
as I primarily thought
And I used a RewriteRule to 301 Redirect all directory requests than do not end with a /
I had a similar problem but could not find a solution over mod_rewrite (did not know how to add this slash with a 301 redirect - tried everything).
What I did is to define:
<IfModule mod_dir.c>
DirectorySlash Off
</IfModule>
ErrorDocument 404 404.php
If now the trailing slash is missed, 404.php is used redirecting the client if the url has no trailing slash now:
<?php
$home="http://".$_SERVER['HTTP_HOST'];
$url=$home.$_SERVER['REQUEST_URI'];
?><html>
<head>
<title><?php echo $_SERVER['REDIRECT_STATUS']; ?></title>
<?php if(!preg_match('/^.+\/$/', $url)) { ?>
<meta http-equiv="refresh" content="0; URL=<?php echo $url; ?>/">
<? } ?>
</head>
<body>
<h1><?php echo $_SERVER['REDIRECT_STATUS']; ?></h1>
</body>
</html>
It smells like hell but worksforme ...

How to write a url rewrite in nginx?

I want people type in http://www.myweb.com/like/1234456 will redirect to
http://www.myweb.com/item.php?itemid=1234456
I wrote something like this in the config but it doesn't work.
location = ^~/like/ {
rewrite ^1234456 ../likeitem.php?item=1234456break;
return 403;
}
this is just a test. I haven't used the $ matching yet.
I also restart my ngnix server but still.. it doesn't do the redirect.
The code above will not work because of a missing $ and poor use of the return command.
The code below works with Nginx, including version 0.8.54.
Format below is :
DesiredURL
Actual URL
Nginx_Rule
They must be inside location / {}
http://example.com/notes/343
http://example.com/notes.php?id=343
rewrite ^/notes/(.*)$ /notes.php?id=$1 last;
http://example.com/users/BlackBenzKid
http://example.com/user.php?username=BlackBenzKid
rewrite ^/users/(.*)$ /user.php?username=$1 last;
http://example.com/top
http://example.com/top.php
rewrite ^/top?$ /top.php last;
Complex and further
http://example.com/users/BlackBenzKid/gallery
http://example.com/user.php?username=BlackBenzKid&page=gallery
rewrite ^/users/(.*)/gallery$ /user.php?username=$1&page=gallery last;
Try this,
server {
server_name www.myweb.com;
rewrite ^/like/(.*) http://www.myweb.com/item.php?itemid=$1 permanent;
}

Resources