apache htaccess to nginx rules - apache

I have these htaccess rules and I want to convert them to nginx. I have tried online converters and different approaches but nothing seems to work. I am curious to see other opinions, to compare with everything I did by now and see if something will work in the end.
Here is the code from the apache .htaccess file:
RewriteEngine on
Options -MultiViews
RewriteCond %{HTTP_COOKIE} HTTP_IS_RETINA [NC]
RewriteCond %{REQUEST_FILENAME} !#2x
RewriteRule ^(.*)\.(gif|jpg|png)$ $1#2x.$2
# if #2x isn't available fulfill the original request
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)#2x\.(gif|jpg|png)$ $1.$2

Related

URL Rewriting in .htaccess (Apache) displaying 404-Error

this is my first question here on stackoverflow because in the past I always found a question that described my problem perfectly.
But now they were not able to do that, so I decided to ask for help myself.
My goal is to display profiles, but the url shouldn't look like "/profile/show-profile.php?user=admin", just "/profile/admin".
So looked it up on google and found URL rewriting to be potentially useful, by editing the .htaccess file.
The problem is, it doesn't work. I already have some things in my .htaccess (redirecting to https and the 404-Page "/pagenotfound.php") and it seems like they don't work in combination.
# https redirecting
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://int-politics.com/$1 [R=301]`
# 404 page
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /pagenotfound.php
ErrorDocument 404 /pagenotfound.php
# URL REWRITING
RewriteEngine On
RewriteBase /profile/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ show-profile.php?user=$1
When I add the URL Rewriting part the 404-Page doesn't work anymore. Every site that doesn't exist just outputs "/pagenotfound.php" (see image -->)
Not-existing site just outputs /pagenotfound.php instead of showing it.
And the url-rewriting doesn't work too.
It would be wonderful if you could help me with this problem and tell me whats wrong. Thank you very much!
You usage of the RedirectBase is wrong. It should appear only once in such a distributed configuration file. Actually it is not required in this example at all ... Please take a look into the documentation for details on that: https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
Also it is vital to understand that the rewriting engine loops if a rule gets applied. And why that makes using the L or the END flag so important.
That probably is what you are looking for:
RewriteEngine On
RewriteBase /
# https redirecting
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^ https://int-politics.com%{REQUEST_URI} [R=301,END]
# profile rewriting
RewriteRule ^/?profile/(\w+)$ /profile/show-profile.php?user=$1 [END]
# 404 page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /pagenotfound.php [END]
ErrorDocument 404 /pagenotfound.php
Best is to implement such rules in the central http server's host configuration. If you do not have access to that (read: if you are using a cheap hosting provider) then you can use a distributed configuration file instead, if the consideration of such files has been enabled (see the documentation for the AllowOverride directive on that).

Convert links using Apache without redirection

I need to convert links from /s/categories/... to /s/blog/categories/... without redirection
For instance, from:
https://example.com/s/real-estate/whatever-else-links/
to:
https://example.com/s/blog/real-estate/whatever-else-links/
Similarly there are few other categories like real-estate that need to be converted from /s/categories/ to /s/blog/categories/
To make things simple I can enter the other categories logic separately since there are few.
Upon some research I tried the following in the .htaccess file that does not work:
RewriteCond %{HTTP_HOST} !^/s/real-estate/([^\.]+)$
RewriteRule ^/s/barcelona-real-estate/([^\.]+)$ /blog/real-estate/$1
Here is the rest of the .htaccess file is as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /s/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /s/index.php [L]
RewriteCond %{HTTP_HOST} !^/s/barcelona-real-estate/([^\.]+)$
RewriteRule ^/s/barcelona-real-estate/([^\.]+)$ /blog/barcelona-real-estate/$1
</IfModule>
Please let me know how to convert the links at the web server level. Thanks very much.

.htaccess URL rewrite not working, tried all possible options

I was trying to rewrite a URL for making my site SEO friendly, but .htaccess rewrite not seems to work.
My URL is
www.tasteofkochi.com/dine-detail.php?a=150
I need this to look like
www.tasteofkochi.com/sometext/150
I did the simple formula but it's not reflecting, intact nothing happens at all. If I add some junk char in htaccess, site crashes, which means htaccess is working fine. Also I added a formula to remove .php extn, and that too works fine. Only issue is with this one. Can anyone please help me. I enable rewrite in httpd and allow all in directories, still not working.
Below is my .htacces
RewriteEngine on
Options +FollowSymLinks -MultiViews
RewriteBase /
## hide .php extension
# To externally redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteRule ^detail/([0-9]+)/?$ dine-detail.php?a=$1 [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?p=$1 [L,QSA]
We can create pretty urls with .htaccess and php by mainly two files one is .htaccess and another index.php
Example

override apache rewrite rules with htaccess

working on a server that hosts multiple domains.
Somewhere in apache config is a rewrite rule that is set for ALL domains.
What happens is if a user goes to example.com/foo they are supposed to get redirected to example.com/foo_bar
However, in one domain, I want to override this behavior so that the url stays at example.com/foo and does not redirect. I've been searching and trying various rules and conditions to no avail. I don't have access to the apache config, but I am using .htaccess for some rewrite rules on this domain.
here's my rewrite rules in .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
# this isn't working
# RewriteRule ^(foo)($|/) - [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1
# this didn't work either
# RewriteRule ^/foo index.php?/$1 [L]
</IfModule>
Try redirecting /foo_bar back to /foo.
When Apache processes the rewrite rules it runs through them multiple times, which you can see when you turn on debugging. I think what's happening in your case is that you're trying to match the URI in the first pass, but Apache has already modified it to /foo_bar.
Also, as a matter of debugging, you should try to recreate the problem in an environment you control. Ask your sysadmin for a copy of the global configuration and mirror the set up you're constrained to.
You can create exception for one domain:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^(?:www\.)?domain\.com$ [NC]
RewriteRule ^foo(/.*)?$ /foo_bar$1 [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

Folder control with .htaccess

I have a directory /public_html/myfolder/ and I want my domain www.example.com to point to /public_html/myfolder/ as my root folder.
I figured that out, but problem is, my images are not showing.
Here's my .htaccess file:
Options -Indexes
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .* https://www.example.com/$1 [L,R=301]
RewriteRule ^$ myfolder/index.php [L]
# Require SSL
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST} [L]
# Rewrite rules
<IfModule mod_rewrite.c>
#RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ myfolder/index.php?q=$1 [L,QSA]
</IfModule>
The issue may be that you have relative paths specified for your images on pages and after the URL is rewritten the links get broken. To fix this cosider using root-relative paths for the resources.
You will need to make sure the images are ignored by using RewriteCond's.
Try something like
RewriteCond %{REQUEST_FILENAME} !\.(jpg|png|gif)$
(not sure if the . should be escaped, but i think it should be)
You can also try adding
RewriteLog
RewriteLogLevel 3 (or maybe greater)
I'm not sure if this works if it is inserted into a .htaccess, it might be that it has to be in the server conf, but it is worth a try. It is easier to debug rewrite if you have the log.