Hi A2,
You might benefit from reading the mod_rewrite tutorial http://dk.co.nz/seo as it contains explanations and sample code. It’s helped may members and should help you, too.
That article covers almost everything you’ll need to know about mod_rewrite:
-
You should be redirecting from a URI format to a file which Apache can serve (you stated the problem backward but appear to be redirecting to .inc.php files).
-
There are specific characters within regular expressions which require escaping with the backslash ( \ ) and you’re on OVERKILL. They are, dare I say, NEVER used in the redirection!
On the other hand, you’re to be complimented on the check for an existing file before attempting to match and make a redirection.
Compliments, too, for NOT using the
EVERYTHING
atom which is the most common problem newbies make.
THEN, before starting to code, where do you intend to put the .htaccess code for your redirection? It appears that localhost is the DocumentRoot (Apache’s http_docs folder?) and I have to admit that I prefer to use the .htaccess file in the DocumentRoot when it has general applicability. Because it seems as if you are using mainfolder, I’ll suggest code for that directory.
/mainfolder/article => /mainfolder/inc/article.inc.php
Working from YOUR code:
[code]RewriteEngine On # great
RewriteCond %{REQUEST_FILENAME} !-f # even better
RewriteRule /inc/([A-Za-z]+)$ /inc/$1.inc.php [NC,L] # many problems
First, because you’re not in the DocumentRoot, you may need the leading /
(NOT escaped and NOT in the redirection as that specifies the SERVER’s root then,
if the file is not found there, the DocumentRoot)
Second, the second / should not be escaped either
Third, IF article is only lowercase and capital letters, GREAT!
Fourth, NO backshashes in the redirection!!!
Fifth, are all your article files actually article.inc.php?
I ask this because the inc is generally reserved for an included script
Finally, NEVER use the No Case flag in a RewriteRule (which only tests the case sensitive {REQUEST_URI} variable.
NC is designed to be used on the {HTTP_HOST} which is NOT case sensitive.
Besides, you were smart enough to have specified both uppercase and lowercase in your regex so the NC is superfluous[/code]
The result would be:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule inc/([A_Za-z]+)$ inc/$1.inc.php [L]
Now, that code will NOT redirect index to index.php. If you look back at the Fifth comment above, you should be able to see why: The lack of the inc directory and the embedded.inc file extension!
That means that you will need to modify your regex to make it applicable to article.inc.php and index.php (I’ll not go into that here as that code would be more complex) so you’ll need to repeat the above code but omit the .inc in the redirection (I would also recommend specifying inc/index in the regex and inc/index.php in the redirection). Simply add:
RewriteRule index$ index.php [L]
I have preached for years that, when starting to code a mod_rewrite direction, you should start with a simple word explanation of what the code is supposed to do. That means specifying the {REQUEST_URI} to be matched (including exclusions - useful for RewriteCond exclusions) and then the redirection target. When you can do that, the coding becomes trivial.
Regards,
DK