How can I convert this Apache code to work in NGINX? - 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;

Related

Redirecting Dynamic URLs with Multiple IDs using .htaccess (Wordpress)

I've just moved my site onto Wordpress, which means the URL structure has now changed. I'd like to use a Rewrite Rule in the .htaccess file to redirect dynamic URLs with multiple IDs in one single statement, but am not having any success. An example is:
OLD URL: example.co.uk/seasons/season.php?ID=1819
NEW URL: example.co.uk/seasons/1819
The statements I have tried in .htaccess are:
RedirectMatch 301 ^/seasons/season.php?ID=(.*).htm$ example.co.uk/seasons/$1
and
RewriteCond %{QUERY_STRING} ^?ID=1$
RewriteRule ^/seasons/season.php$ example.co.uk/seasons/? [R=301,L]
In neither case, the redirect fires. Is there something about a Wordpress .htaccess file that I'm not considering, or is the error with the statements I'm attempting. I'm using .htaccess 301 redirects for http to https and non-www to www without issue.
This is the fixed version of the approach you chose, to make an external redirection:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)ID=(\d+)(?:&|$)
RewriteRule ^/?seasons/season\.php$ https://example.co.uk/seasons/$1 [R=301,QSD]
Since both, the old and new URLs use the same host name you can simplify that:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)ID=(\d+)(?:&|$)
RewriteRule ^/?seasons/season\.php$ /seasons/$1 [R=301,QSD]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
These rules will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Can't access file with RewriteRule in .htaccess

I have a folder Storage/static/css. And I want for design/css to see Storage/static/css content.
/Classes
...
...
/Storage
/static
/css
/home
design.css
/menu
/footer
/js
...
/uploads
...
...
index.php
I tried this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -Indexes
</IfModule>
RewriteEngine On
RewriteRule ^design/css/(.*)$ /Storage/static/css/$1
RewriteRule ^ index.php [L]
</IfModule>
For index.php works fine for a long time ago.
But for the other one doesn't work at all.
I want for example:
For design/css/home/design.css to return content from Storage/static/css/home/design.css.
Edit:
My entire web app isn't in root. It is inside a folder called /arshwell.
Looks like you accidentally re-rewrite the css requests in a second go...
Have a try with this slightly modified version:
RewriteEngine On
RewriteRule ^/?design/css/(.*)$ /Storage/static/css/$1 [END]
RewriteRule ^ /index.php [END]
This will prevent that requests rewritten to /Storage/static/css/$1 will immediately get rewritten again to index.php by the next rule.
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule set will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and in case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

.htaccess routing to subdirectory on server? performance/loading times

I wonder if it is possible to use an .htaccess file to rout my domain directly to a subdirectory on my server?
I bought a webhosting package from a regular webhoster where my domain.com is connected to my root directory of my server. I wonder if it is somehow possible to upload a .htaccess file to my root directory on my server that automatically routs domain.com (/index.php or /index.html) to domain.com/some-directory/ …
And of course I don't want the addressbar to update to domain.com/some-directory. I want my regular domain just to grab its files from the subdirectory instead of my root directory.
Just for better overview on my server.
So again, when calling mydomain.com it renders index.html (or .php) from my root directory on my server. However I want mydomain.com to render /subdirectory/index.html.
If this is possible, how can I do it? Is this bad in some way? Is there something I didn't think of, e.g. are there longer loading times or anything like that with that approach?
Thank you in advance.
Matt
update: or is this possible with a DNS setting on my web-admin-panel. I'm able to edit all DNS entries.
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/some-directory/
RewriteCond %{DOCUMENT_ROOT}/some-directory%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/some-directory%{REQUEST_URI} -d
RewriteRule ^(.*)$ /some-directory/$1 [L]
Keep in mind if you try to access a directory and are missing a trailing slash, mod_dir will redirect the browser to the URL with the trailing slash and will expose "/some-directory/".
EDIT
Additionally, the 2 -f and -d conditions are to make sure the request is actually for a resource that exists. This isn't required, and if you have further htaccess files with rewrite rules, you probably don't want these. They ensure that if someone requests some bogus URL, the 404 message will be handled outside of the /some-directory/ directory. Otherwise the 404 message will say something along the lines of:
The requested URL /some-directory/blahblahblah was not found on this server.
Exposing that everything is being routed through /some-directory/.

Path issue in wordpress blog

We are having some issue with our relative path in wordpress.
Earlier our application was like http://www.skill-guru.com/skill .
So if we type the blog address as http://www.skill-guru.com/blog it would add a / at end and open it as
http://www.skill-guru.com/blog/
Now our application opens as root in domain http://www.skill-guru.com.
Our blog is opening as http://www.skill-guru.com/blog/ but not as http://www.skill-guru.com/blog.
I am not able to understand the reason.
because of this issue , search is also not working.
Can anyone please help me understand what has changed and how it can be fixed ?
I'm not sure about the underlying cause, but while you figure that out you may just want to redirect "blog" to "blog/" in your .htaccess file. I think this will do it...
RewriteCond %{REQUEST_URI} ^.*/blog$
RewriteRule ^(.+)$ $1/ [R=301,L]
I'm assuming that somewhere else in the .htaccess you have...
RewriteEngine On
RewriteBase /
In fact, you might find that in the process of changing you site someone nuked the existing .htaccess.
If I visit http://www.skill-guru.com/blog, I get the error:
HTTP Status 404 - /blog
type Status report
message /blog
description The requested resource
(/blog) is not available. Apache
Tomcat/6.0.16
This implies that there might be url-rewriting in place or server configuration does not allow you to strip the trailing backslash, you should consult with server support theam.

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