Convert htacces to nginx - nginx

I want to rewrite the following rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(js|ico|gif|jpg|png|css|ttf)$ index.php
Update: I'm looking for an answer without if statement because of performance. if is evil!

Related

Target URL's with a .html extention, remove the extension and add a trialing slash

I'm trying to overwrite only the URLs that end in .html to have the extension removed and then add a trailing slash to the end.
This should result in something like foo.com/bar.html to foo.com/bar/ and still retrieve the html file in the root of the directories.
The below code is working but it can no longer find the .html file to load.
This is on WordPress and I'm thinking this is very important to understand to make this work correctly.
#test (does what it needs to but does not grab the file)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^/]+)/?$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ $1_$2.html [L]
# end test
Don't worry! Simple copy and paste error on RewriteRule ^([^/]+)/?$ $1.php needs to be RewriteRule ^([^/]+)/?$ $1.html
#test (does what it needs to but does not grab the file)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^/]+)/?$ $1.php # Obviously needs to be .html, not .php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ $1_$2.html [L]
# end test

How to remove last part of URL to avoid 404

I realize there are similar questions but the cases aren't the same. I am looking for a way to remove the last part of a url that no longer exists, but is being linked to from some external sources, to reduce high CPU use. The url would look something like this:
http://example.com/2018/12/05/article-name/removethis
Where removethis is what needs to be deleted to avoid 404 errors. I have seen answers to change a part of the url to another, but not how to simply erase a particular string from the end of the url.
I am also reading htaccess documentation, but it is very extensive and I haven't been able to come up with a way to change a rewrite command from the other examples to do what I need.
Thank you for your time.
I think in this case you should specify what you need in case of error and that is various from one to another one and then go around with specific code to handle it as you want . for example the following code will handle error request level by level but of course it is limited up to /1/2/3/4/5 so , more than this levels will be handled by starting form latest one and so on :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)/(.*)/(.*)/(.*)/(.*)$
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)$ /$1/$2/$3/$4 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)/(.*)/(.*)/(.*)$
RewriteRule ^(.*)/(.*)/(.*)/(.*)$ /$1/$2/$3 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)/(.*)/(.*)$
RewriteRule ^(.*)/(.*)/(.*)$ /$1/$2 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)/(.*)$
RewriteRule ^(.*)/(.*)$ /$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule ^(.*)$ / [L,R]
So , the request http://example.com/2018/12/05/article-name/removethis will be http://example.com/2018/12/05/article-name/ if article-name/ directory is exist , otherwise will go to http://example.com/2018/12/05/ if 05/ directory is exist and so on .
Note: the code above could be summarized more by the following form:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)/(.*)/(.*)/(.*)/(.*)$
RewriteRule ^ /%1/%2/%3/%4 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)/(.*)/(.*)/(.*)$
RewriteRule ^ /%1/%2/%3 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)/(.*)/(.*)$
RewriteRule ^ /%1/%2 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)/(.*)$
RewriteRule ^ /%1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule ^ / [L,R]

.htaccess redirect from non existing file

I have a Wordpress installation and what I'm trying to do is the following:
http://domain.com/page.php to redirect to http://domain.com/page/ only if page.php does NOT exists. If it does, it should load page.php
I was thinking about something like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /$1/ [L]
But sadly, it doesn't work. And also few more similar rules to this doesn't have any effect or return 500 error.
Try this rule as your very first rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.(?:html?|php)$ /$1/ [L,NC,R=301]

Mod Rewrite Create Fake Or Hide Wordpress Directory

I would like to fake or even hide my wordpress directory from
mysite.com/wp-content/themes/twentyfourteen/page.php to mysite.com/directory/page.php
or even mysite.com/wp-content/themes/twentyfourteen/page.php to mysite.com/fake1/fake2/fake3/page.php
Within my .htaccess file im using the following commands:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^wp-content/themes/twentyfourteen/([^/]+)$ /fakedirectory$1 [L]
I;ve also tried
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^wp-content/themes/twentyfourteen/([^/]+)$ /fakedir1/fakedir2/fakedir3$1 [L]
But for some reason this is not working... Any help would be appreciated!! Thanks
You're doing the opposite.
Try this way (assuming your htaccess is in root folder)
RewriteCond %{THE_REQUEST} \s/wp-content/themes/twentyfourteen/([^\s]*) [NC]
RewriteRule . /fakedirectory/%1? [R=301,L]
RewriteRule ^fakedirectory/(.*)$ /wp-content/themes/twentyfourteen/$1 [L]

How to redirect to sitemap.php from htaccess

I am using htaccess coppied from wordpress.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
It works fine.
I would like to add a script to redirect to sitemap.php file if user requests sitemap.xml.
I tried adding RewriteCond %{REQUEST_FILENAME} !-^sitemap.xml$ but didn't work.
Put the rule BEFORE that set of rules:
RewriteRule sitemap\.xml sitemap.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Resources