Apache to NGINX Rewrite Issue - apache

I'm trying to convert this apache ReWriteRule to NGINX but it doesn't seem to be working.
Apache Rule: RewriteRule ^([A-Za-z0-9_-]+)\.php$ index.php?pagename=$1 [NC,QSA]
NGINX (not working): location / { rewrite ^/([A-Za-Z0-9_-]+)\.php? /index.php?pagename=$1; }
I do have the NGINX rule in a location block.
What am I missing here? I looked at the other similar questions but didn't find enough suitable clues to solve the problem. Thanks for the help.

Using converter:
server {
server_name example.com;
rewrite ^/([A-Za-z0-9_-]+)\.php$ /index.php?pagename=$1;
}
And indeed, many rewrites are easier with just a rewrite placed in the server {} context (no need for extraneous location).

Related

Apache rewrite - Virtual dir to php script

Following the accepted answer to this post, I constructed a rewrite rule which is pretty straight forward, but doesn't seem to work as designed.
# /st doesn't exist, it's virtual
example.com/st/sometext
Rewrites to
# /app is a symlink to /app.php, and this link works if entered directly
example.com/app/st/sometext
And the rewrite rule:
RewriteRule ^st/(.*)$ app/st/$1 [L]
This rule is at the top of the rewrite block, after which there are other rules, but those rules shouldn't apply, due to the [L] constraint.
The rewrite log file has this:
(2) init rewrite engine with requested uri /st/sometext
(1) pass through /st/sometext
It doesn't seem like the rewrite rule is matching the url. What am I missing?
Ok, I resolved the issue by switching over to nginx (lol) and implementing this rewrite rule:
location /st {
rewrite ^/st/(.*)$ /app.php/st/$1 last;
}
The switchover to nginx really had little to do with the rewrite rule and more to do with getting away from apache 2.2 (default upstream version for centos), however with nginx, rewrite rules are more straight forward than apache's (imo).

How to convert this Apache RewriteRule in Nginx?

I've got this RewriteRule, And I can't make it work with nginx :
RewriteRule ^/espace-annonceurs/##contenu-des-espaces-annonceurs http://mywebsite.com/##contenu-des-espaces-annonceurs [L,P]
The Proxy is because it make an ajax request.
I've tried a lot of things, with location, proxy_pass, rewrite, and so on...
Thanks by advance.
Actually you need proxy request to your host to some other host. That's job for proxy module. The simplest config would be like this:
location /espace-annonceurs/##contenu-des-espaces-annonceurs {
proxy_pass http://mywebsite.com/##contenu-des-espaces-annonceurs;
}
Probably you'll need some proxy_set_headers directive.
Read documentation http://nginx.org/r/proxy_pass.
So a simple rewrite would do.
rewrite ^/(espace-annonceurs/##contenu-des-espaces-annonceurs)$ http://example.com/$1;

url rewrite using Nginx in front of an apache

I'm trying to figure out how to re-write urls using nginx in front of an apache.
I am new to a set up like that and after an extensive research I couldn't figure it out.
I am trying to enable seo friendly urls in a prestashop 1.6.0.6 installation without any luck. The truth is that this is really straightforward when using only an apache as a web server.
I would appreciate it if someone could help me on this.
Whether this will work depends on how your Apache server is configured to accept the URLs. If Apache is configured, as you mentioned with a .htacess file, to serve at the root of the host name, then rewriting may not be required. An example Nginx server block like this:
server {
server_name nginx.example.org;
location / {
proxy_set_header Host $host;
proxy_pass http://apache.example.org:80 break;
}
}
will pass the exact host and path being accessed from Nginx through to Apache without any changes. The server_name and proxy_pass directives will need to be changed for your local configuration, however. In this case, because of the use of the location / {}, all paths are accepted and proxied.
As long as the backend Apache is configured correctly and accessible from Nginx, this should work. The best test would be to ensure that you can access resources on Apache directly first, especially those with the SEO-friendly URLs, which would indicate the .htaccess file is in working and effect. Then configure Nginx in front as per the above.
As for potentially using only Nginx, you could port the rules from the .htaccess over into rewrite directives within Nginx configuration. In my experience, the rules are very similar in functionality and structure:
Apache: RewriteRule ^/(.*\.jpg)$ /images/$1 [L]
Nginx: rewrite ^/(.*\.jpg)$ /images/$1 last;
More information is at the Nginx wiki.

NGINX - Rewrite fancy url

I thought rewrite in Nginx would be straight-forward. It looks like its not.
I would like some help from you guys. My previous setup was Apache2 + PHP. My new setup is Nginx + PHP-FPM.
I would like to transform a simple virtualhost from Apache2.
The directory structure is:
/api (this is root)
VERSION (1)
--- api.php
--- .htaccess
Fx. domain.tld/1/
The .htaccess files looks like this:
RewriteEngine on
RewriteRule ^([a-z]+)/([a-z]+)/api.json$ api.php?app=$1&sapp=$2 [L,QSA]
RewriteRule ^([a-z]+)/api.json$ api.php?app=$1 [L,QSA]
My new Nginx server file looks like this:
server {
listen 80;
root /var/www/domain.tld/api;
index index.php index.html;
server_name domain.tld;
location /1/ {
rewrite ^/([a-z]+)/([a-z]+)/api\.json$ /api.php?app=$1&sapp=$2;
rewrite ^/([a-z]+)/api\.json$ /api.php?app=$1;
}
location ~ \.php$ {
include php5;
}
}
But it does not works when I try to view: domain.tld/1/view/api.json or domain.tld/1/view/extended/api.json
Can anyone explain and give some examples to fix this?
Thanks in advance.
Take a look at how NGiNX processes location blocks, here: http://nginx.org/en/docs/http/request_processing.html
As you can see in your case the request is only served by your location /1/ block, as that is the only one of the two that matches. Problem is, NGiNX only uses one location block per request, so once it got into the location /1/ block, it will not go to the other location block later.
The solution here is to also include your php5 file in the location /1/ block at the end.
location /1/ {
rewrite ^/([a-z]+)/([a-z]+)/api\.json$ /api.php?app=$1&sapp=$2;
rewrite ^/([a-z]+)/api\.json$ /api.php?app=$1;
include php5;
}

Amazon EC2 + 301 www redirection

I'm running a site on AmazonEC2 instance, and so far everything is ok, except a single glitch. When I navigate to
www.mysite.com/somepage.html
, browser happily opens the desired page. But when I try
mysite.com/somepage.html
, I go to the root of my site, i.e. to
www.mysite.com
LiveHTTPHeaders tells me that 301 redirection occurs. So here's the question: what configuration files do I need to change to make redirection respect URL path?
Also, I noticed that nginx does the redirection. Does it mean that redirection is happening outside of EC2 instance or not?
Thank you in advance.
You should have some kind of settings that is redirecting your request from http://mysite.com/ to http://www.mysite.com/
Following is kind of conf. settings you should find in your nginx.conf or inside /etc/nginx/sites-enabled/<site-vhost-name>
server {
server_name example.com;
rewrite ^/(.*) http://www.example.com/$1 permanent;
}
This tells NGINX to redirect request from http://mysite.com/ to http://www.mysite.com/.
If yes?, then blindly remove those two lines of conf.