How do i convert this Mod_rewrite rule to nginx - regex

This is the Htacces rule:
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ ir.php?id=$1
how should I pass it to a Nginx compliant rewrite rule... i read the doc and did this:
rewrite ^([A-Za-z0-9-]+)/?$ ir.php?id=$1 last;
But didnt work.
and another question:
Is there any equivalent of .htaccess to Nginx (per directory rules)
Thanks

As an answer to your second question: no, there is no equivalent for Apache's .htaccess files for per directory configuration. You can have per directory configuration files included via the master config, but Nginx would need to be reloaded every time a config include file changed.

Finally, could solve it... rewrite ^([A-Za-z0-9-]+)/?$ ir.php?id=$1 last; To rewrite ^/([A-Za-z0-9-]+)/?$ /ir.php?id=$1 last;

Related

Insert a directory at the start of a URL in Apache Config

Apologies if this question has already been asked before. I couldn't find an example that handled my exact situation.
I have an Apache Server and have access to the httpd.conf file.
I have a domain (say www.example.com) and I want to insert a directory (say test) after the domain.
So for example I want www.example.com to be mapped to www.example.com/test and www.example.com/folder to be mapped to www.example.com/test/folder and so on.
I have achieved this using the a RedirectMatch directive like this:-
<VirtualHost *:80>
ServerName www.example.com
RedirectMatch ^/$ test/
</VirtualHost>
However this changes the URL in the browser to include the test folder and I would like to keep this hidden from the end user.
I have tried using a rewrite rule but my lack of regex knowledge has let me down here! This is what I have tried (within the virtual host element):-
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^(.*)$ /test/$1
I have tried various combinations with this but nothing seems to work!
Any help would be much appreciated.
Ok, I found the problem here. This was working ok but my browser cache (Firefox) was remembering the old values.
I turned of caching in the browser (by going to about:config and setting browser.cache.disk.enable = FALSE
Then everything started working correctly.
Hopefully this will help others who have the same issue!

.htaccess: Rewrite entire URL if specific directory is called

I'm 90% there but I'm missing something obvious. I am doing local web development of a site. The media directory of the site I'm working on is huge - upwards of 15 GB. Instead of copying that and having to update it periodically just for the sake of viewing this images in my local environment, I want to redirect any requests into the media/ directory back to the production site.
From this...
localhost:8888/sitefolder/media/*
To this...
www.productionserver.com/media/*
This is what I have so far but I think I need to use the Rewrite Condition directive:
RewriteRule ^media/(.*)$ http://www.productionserver.com/media/$1[QSA,R=301,L]
Thank you in advance.
Some syntax issues, you need a space after $1 and QSA isn't needed:
RewriteEngine On
RewriteRule ^media/(.*)$ http://www.productionserver.com/media/$1 [NC,R=301,L]
Make sure this is placed in sitefolder/.htaccess
Make sure this is first rule after RewriteEngine On

Disable HTTP trace in .htaccess

Is it possible to turn off HTTP trace method via .htaccess using this directive?
TraceEnable Off
When I try to add this directive in .htaccess I get an internal server error, maybe it's only allowed in my main httpd.conf file?
Unfortunately this directive isn't allowed in .htaccess as per official docs:
TraceEnable Off
You need to put this in Apache config or your vhost setup.
In .htaccess you can use a workaround (as per the link shared by #donald123):
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^TRACE
RewriteRule ^ - [F]
take a look on this http://www.ducea.com/2007/10/22/apache-tips-disable-the-http-trace-method/ it should be a solution for you

Redirecting home to page in subdirectory, mask url

I'm using wordpress.
I'd like my site's domain root to display contents of another page while still showing my domain root as the url.
EXAMPLE: "http://www.mysite.com" displays contents of "http://www.mysite.com/blogs/home"
but users still see "http://www.mysite.com" in the address bar.
I've managed to get the redirect working, but it's not masking the URL. It essentially is redirecting.
Here's what I have now in my htaccess:
RewriteEngine on
RewriteRule ^\/?$ \/blogs\/home\/ [R,NC]
The R option forces an external redirect. Try just [NC] instead
what about this way.
put the index.php and .htaccess file in the root of your domain.
edit index.php to grab the "wp-blog-header.php" file from what ever location you put it. For example, if your wordpress file is located in blogs/home/ sub directory, you change the index.php file located in the root directory as require('./blogs/home/wp-blog-header.php').
forget about the redirect issue.
I think this is the easiest way. Or did I misunderstand your question?
There is a very simple solutions to this problem. In fact wordpress has an option to accomplish this task.
Go to Dashboard->Settings->Reading->Front page displays
Select a static page as front page.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^$ /blogs/home [NC,P]
PS: Make sure you have mod_proxy enabled in your Apache config.

Mod Rewrite rule to redirect all pdf request to another location

I'm trying to clean up our website. I'm using ISAPI Rewrite. I have figured out the regex I need to select the files in question.
^(?!.*?web_content.*?).*?\.pdf
I want to redirect all pdf requests to look in web_content/pdf/. So The pseudo rule I want is
redirect all requests to pdfs that aren't already being requested from
web_content/pdf. Drop off the original folder path.
/somefolder/this/mycool.pdf ==> /web_content/pdf/mycool.pdf
My question is how would I actually create the Mod rewrite rule? I don't know how to correctly do the replacement command. I also hope this rule wont affect external pdf links on our site.
This should work:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/web_content/pdf/
RewriteRule ^(.+\.pdf)$ /web_content/pdf/$1 [L]
Helicon ISAPI_Rewrite v3 is pretty much the same as Apache's mod_rewrite (except few advanced things), therefore almost all rules that work on Apache will work here as well.