I'm trying to do a rewrite for nginx. It's working but for sub directories it is taking the root's index.php instead of the index.php in the individual subdir. This is how my directory structure looks like:
/index.php
/p/index.php
/c/index.php
and this is my rewrite:
if (!-e $request_filename){
rewrite ^/([^./]+)/?$ /index.php?act=$1 break; #need break?
rewrite ^/([^./]+)/(.+)/?$ /index.php?act=$1&upm=$2 break;
rewrite ^/([^./]+)/([^./]+)/?$ /$1/index.php?act=$2;
}
I tried adding an entry for each subdirectory but it doesn't work either:
location /p/ {
root /home/user/public_html/p/;
rewrite ^/([^./]+)/(.+)/?$ /index.php?act=$1&upm=$2 break;
}
Any ideas?
The rewrite ^/([^./]+)/([^./]+)/?$ /$1/index.php?act=$2; will never be executed as the previous rewrite will always match. You might try swapping rewrites #2 and #3.
The root /home/user/public_html/p/; is probably wrong as /p/ will appear twice as it is in the $document_root and the $uri.
The rewrite in your location /p/ should rewrite to /p/index.php and not /index.php?
Related
Url https://example.com/profile/chico
must be understood by nginx as:
https://example.com/profile/?username=chico (the url that currently works).
https://example.com/profile/ currently works as well, as it currently resolves to index.php, without the username param.
I currently have
location /profile/ {
index /profile/index.php;
}
There are so many different things about this on the web and I didn't find specifically what I need.
Also tried
location /profile/ {
rewrite ^/profile/(.*)$ /profile/?username=$1;
}
Try:
location /profile/ {
try_files $uri #rewrite;
}
location #rewrite {
rewrite ^/profile/(.+)$ /profile/index.php?username=$1 last;
rewrite ^ /profile/index.php last;
}
If the first rewrite matches, the second rewrite is ignored. After the URI is rewritten with a .php extension, the processing moves to a different location block.
See this document for more.
EDIT: Added try_files block to handle static content.
The example below is what I have tried with no luck. I would like to redirect all files in the folder /files/dump/ that start with 15EIP to /file-is-gone
rewrite ^/files/dump/15EIP(*.*)?$ /file-is-gone permanent;
#Starkeen solved this for an Apache configuration at Redirecting the content of a directory, but not the directory itself as well as some of its subdirectories and files, but we'll soon be moving to Nginx.
His very elegant solution for Apache:
RedirectMatch ^/category/((?!index|images|menu)[^/]+)/?$ /$1
Here, everything in the /category/ directory -- except for /category/index.php, the content of /category/images/ and the content of /category/menu/ -- is redirected to the root folder.
We tried the translation from htaccess to Nginx offered at http://winginx.com/en/htaccess,
location ~ ^/category/((?!index|images|menu)[^/]+)/?$ {
rewrite ^(.*)$ /$1 redirect;
}
but that didn't work.
For some reason, when we finally give up searching for a solution and end up asking for help, we often find one soon after. Here it is -- it works perfectly:
rewrite ^/category/((?!index|images|menu)[^/]+)/(.*)$ /$1 permanent;
I need to convert in Nginx these .htaccess rules, but when I try to target the filename and its extensions, Nginx doesn't match it. I really don't understand why of this behavior.
RewriteRule ^(.*?)(test_mod_rewrite) /$1media/plg_jchoptimize/assets/modrewrite.php?q=$2
RewriteRule ^(.*?)(gz|nz)/([^/]+)/([^/]+)/([^/]+)\.(js|css)$ /$1media/plg_jchoptimize/assets/jscss.php?f=$5&type=$6&gz=$2&d=$3&i=$4
I converted these rules in Nginx:
location /media/plg_jchoptimize/assets/ {
rewrite ^/(test_mod_rewrite) /media/plg_jchoptimize/assets/modrewrite.php?q=$1;
rewrite ^/(gz|nz)/([^/]+)/([^/]+)/([^/]+)\.(js|css)$ /$1media/plg_jchoptimize/assets/jscss.php?f=$4&type=$5&gz=$1&d=$2&i=$3;
}
The example URL is:
/media/plg_jchoptimize/assets/gz/30/0/d8604b25d503f1dcbb035ec731857648.css
What I'm wrong?
i need some help from the experts.
I have some rewrite rules:
post.php go to http://example.com/post/3/
RewriteRule ^post/([A-Za-z0-9-]+)/?$ post.php?id=$1 [NC,L]
It Works!
But, How can i add a new rewrite rule to the NEW Page Users.php after post/ID/
And get the ID in Users.php (Users.php?id=3)
Like this http://example.com/post/3/users
Thanks!
If you want /post/3/users/ to go to /Users.php?id=3, you have to put that rule before your existing rule. Your existing rule matches /post/3/' which is a prefix of what this additional rule matches, so that rule will never fire if it is after.
# catch the longer URL first
RewriteRule ^post/([A-Za-z0-9-]+)/users/?$ Users.php?id=$1 [NC,L]
# No /users/ on it; rewrite to post.php
RewriteRule ^post/([A-Za-z0-9-]+)/?$ post.php?id=$1 [NC,L]
Another thing: you tagged your post with .htaccess. Does that mean your rewrites are in a .htaccess? If so, you should use RewriteBase because your rewrites are relative. Why it's working is probably that you are allowing a 1:1 correspondence between paths and URLs on your webserver.
In a per-directory context like .htaccess, mod_rewrite is working with path names, not URLs. But if you do a relative rewrite, the path is turned into a URL and fed back into the Apache's request handling chain to be processed over again. How the path is turned into a URL is that the contents of RewriteBase are added to the front. If you don't have RewriteBase then a silly thing happens: the path to your directory (that was removed for the RewriteRule is just tacked back on!).
Example: suppose your DocumentRoot is /var/www. Suppose the browser asks for the URL /foo. This gets translated to the path /var/www/foo If inside the .htaccess for /var/www/ you rewrite foo to bar (and RewriteBase is not set), then mod_rewrite will generate the URL /var/www/bar: it just takes the /var/www/ directory that was stripped off and puts it back on. Now that can be made to work: just make /var/www/ a valid URL going to that directory. For instance with
Alias /var/www /var/www # map /var/www URL to /var/www directory
But that is hacky. The right way is to have RewriteBase / in the .htaccess. So when foo is rewritten to bar, it just gets a / in front and becomes the url /bar. This is fed back to the server and will resolve back to the document root again "naturally".
I used hacks like that before I understood the rewriting. I even used / as a DocumentRoot! That made everything work out since then most URLs are paths: you don't have to think of URLs a an abstraction separate from your filesystem paths. But it's a dangerous, silly thing.