Mod_rewrite fix for specific file

My page is not redirected because it should due to my .htaccess file, which is set as:

RewriteEngine on RewriteCond $1 !^(index\.php|resources|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

I use this setting for my MVC Framework, so I get URLs like /controller/method/argument , but when I redirect to /forum/login.php it cuts to / forum /.

How to add this as an exception so that I can redirect to /forum/login.php

I found another .htaccess in the my / forum / directory that might cause the problem?

 # BEGIN PunBB <IfModule mod_rewrite.c> # MultiViews interfers with proper rewriting Options -MultiViews RewriteEngine On # Uncomment and properly set the RewriteBase if the rewrite rules are not working properly #RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . rewrite.php [L] </IfModule> 
+8
php apache .htaccess
source share
2 answers

First I will tell you how to read the RewriteRule:

You start with the first (or next) RewriteRule entry:

RewriteRule ^(.*)$ index.php/$1 [L,QSA]

The first parameter is a regular expression that can match your requested URL. ^(.*)$ matches all and saves this “everything” inside the variable, which can be used later.

Only if there are previous RewriteCond entries are they evaluated as follows:

RewriteCond $1 !^(index\.php|resources|robots\.txt)

$1 is a link to the content matched inside the first parentheses of the RewriteRule. This compares with the second parameter, which is a regular expression with a few explicit names, as well ! denies expression, for example. this rule only allows a RewriteRule to be executed if the regular expression does not match. If this condition returns true, the following condition will be considered.

RewriteCond %{REQUEST_FILENAME} !-f

If the requested file name is not a real file on the hard disk, this condition is true.

RewriteCond %{REQUEST_FILENAME} !-d

If the requested file name is not a real directory, this condition is true.

Only if all these conditions are true (they are associated with AND), we return to the rewrite rule:

RewriteRule ^(.*)$ index.php/$1 [L,QSA]

The result of this overwrite step is defined as the second and third parameter. $1 used again, as is the content of the match, and the parameters determine that this rule, if it is initially agreed, will be the last rule (L) and that any query string defined in the rewrite target variable will be added to any query string in the original URL (QSA).

Criticism:

Regular rewriting for MVC frameworks should be as much as possible. All dubbing conditions must be evaluated for successful dubbing. The show will stop only if any of the RewriteCond returns false. Each request that is rewritten undergoes a lot of intense cpu tests. RewriteRule regex first, then the regular expression in the first RewriteCond, followed by two tests of the hard disk in the file system for the existence of the file.

On the other hand, the first RewriteCond seems unnecessary. It checks certain names and, if found, cancels the rewriting. "index.php" must be detected by the second RewriteCond, since it is an existing file (how rewriting will work if not). Everything that starts with “resources” will also be matched, but probably shouldn't for the same reasons: Existing resources will be found by the second RewriteCond. The latest file is "robots.txt". It is always useful to have one, possibly emtpy, if you want to avoid 404 when robots retrieve your site.

Since you are not changing anything in the query string, the [QSA] directive is not needed.

Improvements:

 RewriteEngine on RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [L] RewriteRule ^.*$ index.php [L] 

The first RewriteRule will match the entire requested path. Two RewriteCond are associated with [OR], so the first RewriteCond, which returns true, will cancel further evaluation. The first RewriteCond checks to see if the requested file exists. If it exists, it returns true, and processing returns to the first RewriteRule. The target expression is "-", which means "do not overwrite". [L] stops further processing of rewriting rules. So at the end, for an existing file, we have only one regular expression and one file system test, and then this existing file will be sent to the browser.

If the file is not found, the first RewriteRule and RewriteCond will not start, so [L] will not stop the process. Thus, the second RewriteRule is executed. This is unconditional, and the regular expression is the same as before, matching everything and rewriting it to "index.php".

This rewrite will not invoke your index.php if any file exists, including /forum/login.php.

You can change the second to RewriteRule ^.*$ index.php/$0 [L] if you want to continue parsing $_SERVER['PATH_INFO'] instead of $_SERVER['REQUEST_URI'] .

+21
source share

Try the following:

 RewriteEngine on RewriteCond $1 !^(index\.php|forum|resources|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

And this:

 # BEGIN PunBB # ---------------------------------------------------------------------- # Start rewrite engine # ---------------------------------------------------------------------- <IfModule mod_rewrite.c> # MultiViews interfers with proper rewriting Options -MultiViews RewriteEngine On # Uncomment and properly set the RewriteBase if the rewrite rules are not working properly RewriteBase /forum/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . rewrite.php [L] </IfModule> # ---------------------------------------------------------------------- # Better website experience for IE users # ---------------------------------------------------------------------- # Force the latest IE version, in various cases when it may fall back to IE7 mode # github.com/rails/rails/commit/123eb25#commitcomment-118920 # Use ChromeFrame if it installed for a better experience for the poor IE folk <IfModule mod_setenvif.c> <IfModule mod_headers.c> BrowserMatch MSIE ie Header set X-UA-Compatible "IE=Edge,chrome=1" env=ie </IfModule> </IfModule> <IfModule mod_headers.c> # Because X-UA-Compatible isn't sent to non-IE (to save header bytes), # We need to inform proxies that content changes based on UA Header append Vary User-Agent # Cache control is set only if mod_headers is enabled, so that unncessary to declare </IfModule> # ---------------------------------------------------------------------- # UTF-8 encoding # ---------------------------------------------------------------------- # Use UTF-8 encoding for anything served text/plain or text/html AddDefaultCharset utf-8 # Force UTF-8 for a number of file formats AddCharset utf-8 .html .css .js .xml .json .rss # ---------------------------------------------------------------------- # A little more security # ---------------------------------------------------------------------- # Do we want to advertise the exact version number of Apache we're running? # Probably not. ## This can only be enabled if used in httpd.conf - It will not work in .htaccess # ServerTokens Prod # "-Indexes" will have Apache block users from browsing folders without a default document # Usually you should leave this activated, because you shouldn't allow everybody to surf through # every folder on your server (which includes rather private places like CMS system folders). <IfModule mod_autoindex.c> Options -Indexes </IfModule> # END PunBB 
+2
source share

All Articles