HTACCESS Rule to NGINX Configuration File (One Rule) - nginx

I'm struggling with getting one rule that I had in my HTACCESS file, to work within my NGINX configuration files. I've used the converters online, and searched on Google but can't seem to find a solution that'll work. I feel this would be a quick solution for someone a bit more familiar with NGINX redirects. I sure hope so! :-)
HTACESS Rule:
RedirectMatch 301 ^/services/automotive-services/(.*)$ http://www.domain.com/automotive-services/$1
I'm trying to get my NGINX rule to do the following...
http://www.domain.com/services/automotive-services/ should go to http://www.domain.com/automotive-services/
http://www.domain.com/services/automotive-services/pagenamehere should go to http://www.domain.com/automotive-services/pagenamehere
Any assistance with this would be great! :-)

You have to remember that Nginx does not do anything with htaccess files. Htaccess files are purely an Apache construct.
As for how you would do the RedirectMatch in Nginx, put this in your server configuration:
if ( $request_filename ~ services/.+ ) {
rewrite ^(.*) http://www.domain.com/$1 permanent;
}
This will check if $request_filename contains "services", and if it does, it will rewrite it via 301 to http://www.domain.com/.

Related

nginx how to redirect all pagination

I need help with my nginx configuration.
The goal is to have all requests to my site like site/page1 site/smth/page1 be redirected to just site/ and site/smth/ basically for all requests ending like page[number]
I tried some examples that I found like rewrite ^/page/(.*)$ /$1; still wasn't able to get the redirection. Maybe I misplaced it, not quite sure where I should put the sting. Tried location and server blocks.
The nginx documentation examples for redirecting were a bit too hard to understand for me, so a little explanation would be great.
If you need a 301 HTTP redirection, try this rewrite rule (before the first location block):
rewrite ^(.*/)page\d+$ $1 permanent;
You can try something like this (not tested)
location ~ ^/(.+)/page[0-9]+$ {
rewrite ^/(.+)/page[0-9]+$ /$1 last;
}

WP migration from apache to nginx results -- 404 -- on https

That was kind of a loaded title.
I moved my site over from Apache to Nginx and I'm learning the hard way that Nginx does things its own ways. It does not like htaccess files, ignores them. This causes problems when running Wordpress, because wordpress supposedly loves to use htaccess files and nginx does not. Why? I have no idea.
Anyways,
I managed to figure out how to bring back the site from the abyss of 404, by placing this code in nginx.conf file
location / {
index index.php index.html;
if (!-e $request_filename)
{
rewrite ^/(.+)$ /index.php last;
}
}
But.
while pages load fine on HTTP, HTTPS still shows dreaded 404. Why? I don't know. So, anybody know what to do next?
Well, it turns out I also have to add the same code on nginx.ssl.conf file.
Why? I don't know, but it works.

How can I convert this Apache code to work in NGINX?

I have a file - sitemap.php - which when called shows the same kind of content you might expect from a sitemap.xml file.
I want it so when I go to the URL /sitemap.xml it is actually showing the content that would be showed if you went to /sitemap.php
This used to be achieved by using .htaccess but we don't use Apache now.
RewriteRule ^/sitemap.xml$ sitemap.php [L]
I did try some kind of "apache to nginx" converter thing online but I'm not too fluent in NGINX config code so couldn't say if it was right or wrong.
Sure. Put into your server configuration block before any defined locations this line:
rewrite ^/sitemap.xml$ /sitemap.php last;

nginx: Redirect special characters like "%23" in URL to "#"

My nginx site has a few bad links pointing to it like this:
/some-page%23some-part
/some-page">
This is causing 404's and Google Webmaster Tools is complaining too.
The URL /some-page#some-part does get processed properly and works.
How can I get nginx to redirect the %23 in a URL to #? What about the "> junk?
The links out there cannot be changed, so I'm looking to 301 redirect them myself.
Thanks!
Edit: thanks to Deadooshka for the help. My working solution, rewrite ^(.*)\#(.*)$ /$1#$2 redirect;, is discussed within his answer's comment thread.
not tested. I'm not sure which symbols get the pattern.
rewrite ^/([^\#]+)\#([^\#]+)$ /$1#$2 redirect;
rewrite '^/([^\&]+)\&quot\;\&gt\;$' /$1 redirect;

Timthumb.php Rewrite Seo Friendly URL (Nginx)

I started using nginx today.
These codes was working well rewrite on Apache.
RewriteRule ^thumb/(.*)x(.*)_(.*) styles/timthumb.php?src=http://image.mysite.org/uploads/$3&h=$2&w=$1&zc=1 [NC,L]
Url structure like this
http://www.mysite.org/styles/timthumb.php?src=http://image.mysite.org/uploads/fasulye.jpg&h=134&w=228&zc=1
To
http://www.mysite.org/thumb/228x134_fasulye.jpg
I want make friendly seo link of thimtumb with Nginx. Please help me. :(
Sorry my bad English.
You don't need to change much of the apache format
rewrite ^thumb/(.*)x(.*)_(.*) styles/timthumb.php?src=http://image.mysite.org/uploads/$3&h=$2&w=$1&zc=1 last;

Resources