How to convert this apache rewrite rule to nginx - wordpress

I've been struggling to convert my past apache rewrite rule to nginx (also not sure if I'm placing it in the right place so would appreciate if you can tell me where to place it).
Basically this was apache rewrite rule on my .htaccess file on Wordpress:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?hosts/(.*)$ /user/$1 [R,L]
</IfModule>
As you can see, I'm using this rule in order redirect from example.com/hosts/username to example.com/user/username
I've used this converting tool https://labs.gidix.de/nginx/ and it outputs this conversion: rewrite ^/?hosts/(.*)$ /user/$1 last; - however I tried placing this in Ajenti's(control panel) advanced custom configuration but it's not working.

as an option
location ~ ^/hosts/(.*)$ {
return 301 $scheme://$host/user/$1;
}

You probably need to make this an external redirect so that WordPress will take notice. The cleanest solution would be to place it into a location of its own:
location ^~ /hosts/ {
rewrite ^/hosts(.*)$ /user$1 permanent;
}
See this document for more.

Related

HTACCESS Rule to NGINX Configuration File (One Rule)

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/.

removing just "?m=1" from url using rewrite rule

just few day before i had migrated my blogger blog to wordpress. Now i find crawn error with many url, at the end of many url the name and value is there (?m=1) which shown as a 404 error now i want to redirect all the url additing .htaccess file
example:
http://www.tipsviablogging.com/blogger-tricks/facebook-disqus-tab-in-blogger.html?m=1
musy redirect to
http://www.tipsviablogging.com/blogger-tricks/facebook-disqus-tab-in-blogger.html
any one is having expertise in url rewrite kindly help me...
I haven't got a test system handy, but something like this in your .htaccess should do the trick:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^m=1$
RewriteRule ^(.*)$ /$1? [R=301,L]
If memory serves, you need the ? at the end of the target in the RewriteRule to stop the original query string being appended.
The code assumes you haven't got any other parameters (eg it won't work if you have ?m=1&foo=bar).
I want to add a solution on NginX:
Use below code in "location /" Of VirtualHost config
if ($query_string ~ "^m=1$"){
rewrite ^(.*)$ /$1? redirect;
}

301 Redirect: remove middle folder structure but preserve end file

Wordpress installation.
Indexed Files = http://www.mydomain.com/blog/somecontent
New structure = http://www.mydomain.com/somecontent
Need to remove the /blog/ and 301 redirect to /somecontent across 1000 pages.
Any help would be appreciated as google has indexed previous /blog/ site structure and now giving 404's on new url's.
Thanks
First thing you must do is to go to Wordpress settings and change WP install path to /somecontent then put this code in your .htaccess under DOCUMENT_ROOT directory above your regular WP stuff:
RewriteRule ^blog/(.*)$ /$1 [R=301,L,NC]
Assuming you have mod_rewrite enabled, you might try something similar to
RewriteRule ^blog/somecontent/(.*)$ http://example.com/somecontent/$1 [R=301,L]
this assume the same structure under both folders.

NGINX URL Masking

We have a server where nginx is installed, we have also configured PHP As FastCGI on the server. Everything is working fine except rewrite rule. Our requirement is to mask an URL
for eg:- if someone search in our website the URL which comes will be like http://example.com/search.php?searchword=$1 ($1=searched word) . We need to display URL for our customers as http://example.com/$1.html.
We have set rewrite rule as rewrite ^/(([a-zA-Z_0-9]|-)+/?)$ /search.php?searchword=$1 break;
The URL is getting redirected however we get a file not found error each time. How can we mask the URL just as we do in Apache. Any help would be greatly appreciated
Equivalent Apache htaccess rules which we used are as follows
RewriteCond %{REQUEST_URI} !index\.html$ [NC]
RewriteRule ^([a-zA-Z0-9-/]+).html$ search.php?searchword=$1 [L]
RewriteRule ^([a-zA-Z0-9-/]+).html/$ search.php?searchword=$1 [L]
It was working fine with Apache
You should put this rewrite code to location / . I've tested it on my server.
location / {
rewrite ^/([a-zA-Z0-9/-]+).html/?$ /search.php?searchword=$1 last;
}

How do I create a redirect from a subdirectory to the root domain?

I am trying to redirect all requests to domain.com/drupal to domain.com, including all sub directories in /drupal.
I have seen several answers telling me how to accomplish the opposite of this with .htaccess, but nothing to go this way. I have tried the following line in .htaccess-
RewriteRule /drupal/* ^/(.*)
as well as several variations of the above, based on those answers, but haven't had any luck.
Thanks!
Try this line:
RewriteRule /drupal/(.*) /$1 [QSA,L]
Let me get this straight ... You have an installation of Drupal in <DocumentRoot>/drupal/. You do not want to alter the drupal installation directory, nor you want to change DocumentRoot in your webserver config. You want to redirect any request, for example /foobar.php, into the drupal directory, resulting in maybe /drupal/foobar.php. And all that without exposing the whole stuff to the user. Right so far? OK, I can only assume that you have an Apache webserver, else .htaccess would not work...
First, make sure that you actually are allowed to use .htaccess, so check on the relevant AllowOverride directive in your apache config.
Then try it this way in your <DocumentRoot>/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/drupal.*
RewriteRule ^/(.*)$ /drupal/$1 [P]
RewriteCond ensures that you do not run into an infinite loop. The first part of the RewriteRule is always the URL requested by the client. We prefix the part matched inside the parentheses with /drupal/ and force it to be a proxy request via [P] so that apache would only do an internal redirect (instead of sending the client a "Document has moved" redirection code).
BTW: I did not test it. I may have typos in the code. Read http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule for more information.

Resources