'nginx rewrite' to '.htacces rewrite' - apache

Im very bad in regexps, but in server-configs im the worst.
So any help ll be usefull.
have such in nginx-config
location / {
rewrite ^(.*[^/])$ $1/ permanent;
try_files $uri $uri/ /index.php?$args;
}
need EQUAL thing for .htacces.
Ty.

They should prove very similar.
<IfModule mod_rewrite.c>
RewriteEngine On
# Your web directory root
RewriteBase /
#Make sure it's not a specific file or directory that they're trying to reach
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*[^/])$ index.php?args=$1 [NC, L]
</IfModule>
This will:
Mark RewriteEngine on for the folder the .htaccess is in (if you've got the module enabled);
Specify conditions to avoid redirection if the file is a directory or a file on the server; And
Redirect your Regex to index.php?args={everything caught by the regex}
The [NC, L] flags tell the rule to be not case sensitive and tell Apache this is the last redirection rule, respectively.
I have to admit, I'm not sure what your Regex is testing for though. Would you mind explaining the URL scheme you want to put in place? I should be able to help you formulate the Regex properly.

Related

.htaccess rule for redirecting to parent

I am trying to redirect one of my urls to the parent folder using .htaccess file. I have tried the following rule
RewriteRule ^test/(.+)$ /test/ [L,R=301]
found from htaccess wildcard redirect to parent folder but it is not working (logs show too many redirects).
I also tried the other rules below but none of them worked
RewriteBase /
RewriteCond %{REQUEST_URI} !=/test/
RewriteRule ^(.*) /test/ [END,NC]
or
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^test/(.+)$ /test/ [L,R=301]
The OS is ubuntu server. Any help or pointers is appreciated. Please let me know if I can furnish any other details to debug. Thanks
Following should work considering the parent directory is test
RewriteEngine On
RewriteRule ^(test/).* /$1 [R=301,L]
Add this to disable MultiViews:
Options -MultiViews
The Apache docs on mod_negotiation, describes what the Multiviews Option does, when enabled:
If the
server receives a request for /some/dir/foo and /some/dir/foo does not
exist, then the server reads the directory looking for all files named
foo.*, and effectively fakes up a type map which names all those
files, assigning them the same media types and content-encodings it
would have if the client had asked for one of them by name. It then
chooses the best match to the client's requirements, and returns that
document.
Use:
Options -MultiViews
RewriteEngine on
RewriteRule ^test/(.+)$ /test/ [NC,L,R=301]
In your specific folder
RewriteEngine On
RewriteRule ^rootFName(.*) /rFile.php [QSA,R=301,L]

htaccess to nginx rewrite issue

In my code I am trying to convert Apache .htaccess into Nginx using online convertor. I even edited this file etc/nginx/conf.d/domainname.conf but the rewrite URLS won't work. I have two htaccess files, one of which used in root folder and the second one used in administration folder of my script.
Here is the root's htaccess file content
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ index.php?slug=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ index.php?slug=$1
htaccess converted code for Nginx
location / {
rewrite ^/([a-zA-Z0-9-/]+)$ /index.php?slug=$1;
rewrite ^/([a-zA-Z0-9-/]+)/$ /index.php?slug=$1;
}
Here is the administration folder htaccess file content
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* $0.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^(.*)/$ /$1.php [L,R=301]
Administration folder htaccess converted code for Nginx
location /administration {
if (!-e $request_filename){
rewrite ^(.*)$ /$0.php break;
}
rewrite ^/(.*)/$ /$1.php redirect;
}
I pasted the converted content into domainname.conf (domainname is my domain) and restart the ngnix but it won't work at all. I don't know whether my converted code is accurate or not or any thing else I am missing through it.
Simply edit the file i.e. etc/nginx/conf.d/yourdomain.tld.conf and look for the following code like this:
#location / {
#Uncomment 3 lines and set your rules here!
#}
You need to un-comment these 3 lines of code or paste this code instead:
location / {
if (!-e $request_filename) {
rewrite ^/([a-zA-Z0-9-/]+)$ /index.php?slug=$1;
rewrite ^/([a-zA-Z0-9-/]+)/$ /index.php?slug=$1;
}
}
location /administration/ {
if (!-e $request_filename){
rewrite ^(.*)$ $1.php last;
}
}
Then you must require to restart Nginx, if restarted without any errors it will work accurately otherwise Nginx will stop working, this step is important.

Convert htaccess rules routing paths to index.php to nginx

I'm new to nginx, but I'm trying to convert
# Disallows file browsing
Options -Indexes
# Custom Error Pages
ErrorDocument 403 /error-docs/403.php
ErrorDocument 404 /error-docs/404.php
ErrorDocument 500 /error-docs/500.php
# Turn on the Apache Rewrite Engine
RewriteEngine On
#Conditional to see if it is a real file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If not then pass the query string to index.php and stop any further rules
RewriteRule (.*) index.php/$1 [PT,L]
to nginx syntax. I've tried the suggestion at Routing requests through index.php with nginx but it doesn't do quite what I need it to do. I've also tried the auto converters http://winginx.com/en/htaccess and http://www.anilcetin.com/ but the rules they output don't work for me either. Any help is appreciated.
The issue I had was actually related to my PHP and MySQL code.
location / {
try_files $uri $uri/ /index.php$args;
}
works fine. Thanks for the link.

ReWrite Rules Issue

I seem to be having an issue with my Apache Rewrites
RewriteEngine on
RewriteBase /
RewriteRule ^wordpress/?$ / [NC,L,R=301]
RewriteRule ^/$ wordpress/ [NC,L]
I simply need to remove /wordpress from the URL as I have pages within Wordpress I want to be seen as the main directory
At the moment the urls are
domain.com/wordpress/blog
I'd rather not have /wordpress, rather domain.com/blog
Any help?
RewriteEngine on
RewriteBase /
RewriteRule ^wordpress/(.*)$ blog/$1 [L]
At the moment the urls are
domain.com/wordpress/blog
I'd rather not have /wordpress, rather domain.com/blog
So it looks like you want to redirect the browser if someone makes a request for domain.com/wordpress/ to a URL without the wordpress bit, then internally rewrite the wordpress bit back into the URI? That's definitely do-able but if you have wordpress rewrite rules somewhere they're not going to play nicely with each other at all.
Any rules in the /wordpress directory will supercede any rules you put in the document root, which is where these rules need to go, and your remove-the-wordress-from-URI rules will be completely ignored. Even if you have rule inheritance turned on, the rules in the /wordpress directory will get executed first.
If all of your wordpress rules are actually in the document root's htaccess file, then just make sure to put these before the wordpress ones:
RewriteEngine on
RewriteBase /
# redirect the browser if someone makes a request for domain.com/wordpress/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /wordpress/
RewriteRule ^/?wordpress/(.*)$ /$1 [L,R=301]
# internally rewrite the wordpress bit back into the URI
RewriteRule %{DOCUMENT_ROOT}/wordpress%{REQUEST_URI} -f [OR]
RewriteRule %{DOCUMENT_ROOT}/wordpress%{REQUEST_URI} -d
RewriteRule ^(.*)$ /wordpress/$1 [L]

Mediawiki Subdirectory Installation

Possible Duplicate: MediaWiki on SubDirectory and SubDomain (However doesn't have an answer, nor any replies offering help)
Alright, I'm trying to configure MediaWiki to be installed to a sub-directory. I previously had it installed to a primary domain on http://www.example.com/ with a mod_rewrite using a Short URL of /wiki/Main_Title.
As a note, I'm also on HostGator shared hosting which has special rules for short urls.
My directory structure is as such:
/ (site root; location of .htaccess)
/wiki/ (mediawiki root; location of LocalSettings.php)
Here's what I tried,
.htaccess:
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\&(.*)$ $1\%26$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^wiki/(.+)$ ./wiki/index.php?title=$1 [PT,L,QSA]
/wiki/LocalSettings.php:
## The URL base path to the directory containing the wiki;
## defaults for all runtime URL paths are based off of this.
## For more information on customizing the URLs please see:
## http://www.mediawiki.org/wiki/Manual:Short_URL
##
$wgScriptPath = "/wiki";
$wgScriptExtension = ".php";
$wgArticlePath = "$wgScriptPath/$1";
However, I do not get anything. I simply get a 403/Forbidden error; no 500 Internal Server Error, just a 403 - url http://www.example.com/. It's as if there's nothing being done. I've banged my head against the wall trying to figure this out. Any help is appreciated.
Thanks in advance!
What is the purpose of
RewriteRule ^(.*)\&(.*)$ $1\%26$2
You've lost me entirely on this one. Any URI with a second parameter will loop indefinitely and generate a 500 status return.
If you read the Rewrite documentation:
What is matched?
...If you wish to match against the ... query string, use a RewriteCond with the ... %{QUERY_STRING} variables
The & is normally part of the query parameter. It can appear in the RewriteRule pattern in the case of malformed URI (e.g. fred&q=1). By default, mod_rewrite will treat this as fred?q=1, but this converts it to the escaped %26 variant so this would be passed as a title fred&q=1 to MW (which is an invalid MW title by the way). I think that you should get rid of it or at least understand what you are trying to do here.
The last line should be
RewriteRule ^wiki/(.+) wiki/index.php?title=$1 [PT,L,QSA]
and keep the RewriteBase otherwise mod_rewrite can get confused.
This should work OK:-)
You're on the right track... if you're on a shared environment, then try this:
RewriteEngine on
# Comment to force base to be the subdir:
# RewriteBase /
RewriteRule ^(.*)\&(.*)$ $1\%26$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^wiki/(.+)$ ./wiki/index.php?title=$1 [PT,L,QSA]
Two hints:
If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the .htaccess files), try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)
My ticket to HostGator resolved the issue, albeit un-helpfully. I was hoping for a single .htaccess solution, rather than a double .htaccess redirect/rewrite. However, here's my solution.
/:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^.*$ http://www.example.com/wiki/
/wiki/:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?title=$1 [L,QSA]
I tried different solutions and what worked for me was changing .htaccess in mediawiki subfolder to
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wikifolder/index.php [L]
</IfModule>