how to fix mod rewirte - php

i have problem with mod rewrite
i made htaccess to convert url from php to html
and every thing fine
but problem is some file i dont need to convert like form.php
this is my htaccess
RewriteCond %{REQUEST_URI} !^(.*)form.php(.*)$
RewriteCond %{REQUEST_URI} !^(.*)sitemap\.xml(.*)$
RewriteCond %{THE_REQUEST} ^[A-Z]+\s([^/]+)\.php\s
RewriteRule .* %1.html [R=301,L]
RewriteRule ^([^/]*)\.html$ $1.php
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^.*$ - [L]
RewriteRule ^index.php$ http://%{http_host} [R=301,L]
i dont need convert sitemap.xml and form.php
but i got error when i try to see file form.php
HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
what i can do?

Checking logs is the best place to start as Marc B suggests. You should enable more verbose mod_rewrite logging as so:
RewriteLog "/usr/local/var/apache/logs/rewrite.log"
RewriteLogLevel 3
Don't take the verbosity of the RewriteLogLevel higher than 3.
So, reading your rules, I think I may know what you mean. You should try this:
RewriteCond %{REQUEST_URI} !form\.php$
I do not think Apache cares about the query string that may follow at the URL. There is a %{QUERY_STRING} variable you can use in addition to %{REQUEST_URI}. Unless you have some weird URLs that may have "php" in them, I presume that all of them will end with ".php".
Since "sitemap.xml" seems OK, it is likely the case that you should follow its example and escape out the period character (".") the same way with "\."
A day later, I had some time to think about these rules.
# Use simpler rules, not all that jazz you prepend, appended.
#
RewriteCond %{REQUEST_URI} form\.php$ [OR]
RewriteCond %{REQUEST_URI} sitemap\.xml$
# If %{REQUEST_URI} matches either of the previous rule,
# we skip a certain number of RewriteRules that follow.
# If you add more rules, and need to skip more, you *need* to adjust this number.
RewriteRule . - [S=2]
# Your original line reads:
#
# RewriteCond %{THE_REQUEST} ^[A-Z]+\s([^/]+)\.php\s
#
# Using "%{THE_REQUEST}" variable means you are processing
# a string like this
#
# "GET /something.html HTTP/1.1"
#
# Did you really need the HTTP method and the protocol version?
# Do something simpler. Match the the "http://host/foo/bar/baz/" portion
# Then match the first part of the PHP file name. So, if URL ends in "something.php"
# The second parenthesis will match "something". Then append ".html"
RewriteRule (^.*\/)([^/]+)(\.php)$ $1$2.html [R=301,L]
# **Then** you are trying to rewrite your HTML files to PHP? Why?
# In any case, do something similar as the last RewriteRule.
RewriteRule (^.*\/)([^/]+)(\.html)$ $1$2.php [R=301,L]
# I do not understand RewriteCond %{ENV:REDIRECT_STATUS} 200 at all
# I don't see you using this environmental variable later on in this
# snippet. Recommend the use of "PT" in case you have other stuff running
# that you want to send the rewrite target to be passed back to the
# URL mapping engine.
RewriteRule ^.*$ - [PT, QSA]
# You neglected to add the leading slash in this pattern
RewriteRule ^/index.php$ http://%{http_host} [R=301,L]

Related

Is it possible to rewrite a url upon landing? [duplicate]

Yes, I've read the Apache manual and searched here. For some reason I simply cannot get this to work. The closest I've come is having it remove the extension, but it points back to the root directory. I want this to just work in the directory that contains the .htaccess file.
I need to do three things with the .htaccess file.
I need it to remove the .php
a. I have several pages that use tabs and the URL looks like page.php#tab - is this possible?
b. I have one page that uses a session ID appended to the URL to make sure you came from the right place, www.domain.example/download-software.php?abcdefg.
Is this possible? Also in doing this, do I need to remove .php from the links in my header nav include file? Should IE "support" be support?
I would like it to force www before every URL, so it's not domain.example, but www.domain.example/page.
I would like to remove all trailing slashes from pages.
I'll keep looking, trying, etc. Would being in a sub directory cause any issues?
Gumbo's answer in the Stack Overflow question How to hide the .html extension with Apache mod_rewrite should work fine.
Re 1) Change the .html to .php
Re a.) Yup, that's possible, just add #tab to the URL.
Re b.) That's possible using QSA (Query String Append), see below.
This should also work in a sub-directory path:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
Apache mod_rewrite
What you're looking for is mod_rewrite,
Description: Provides a rule-based rewriting engine to rewrite
requested URLs on the fly.
Generally speaking, mod_rewrite works by matching the requested document against specified regular expressions, then performs URL rewrites internally (within the Apache process) or externally (in the clients browser). These rewrites can be as simple as internally translating example.com/foo into a request for example.com/foo/bar.
The Apache docs include a mod_rewrite guide and I think some of the things you want to do are covered in it. Detailed mod_rewrite guide.
Force the www subdomain
I would like it to force "www" before every URL, so its not domain.example but www.domain.example/page
The rewrite guide includes instructions for this under the Canonical Hostname example.
Remove trailing slashes (Part 1)
I would like to remove all trailing slashes from pages
I'm not sure why you would want to do this as the rewrite guide includes an example for the exact opposite, i.e., always including a trailing slash. The docs suggest that removing the trailing slash has great potential for causing issues:
Trailing Slash Problem
Description:
Every webmaster can sing a song about the problem of the trailing
slash on URLs referencing directories. If they are missing, the server
dumps an error, because if you say /~quux/foo instead of /~quux/foo/
then the server searches for a file named foo. And because this file
is a directory it complains. Actually it tries to fix it itself in
most of the cases, but sometimes this mechanism need to be emulated by
you. For instance after you have done a lot of complicated URL
rewritings to CGI scripts etc.
Perhaps you could expand on why you want to remove the trailing slash all the time?
Remove .php extension
I need it to remove the .php
The closest thing to doing this that I can think of is to internally rewrite every request document with a .php extension, i.e., example.com/somepage is instead processed as a request for example.com/somepage.php. Note that proceeding in this manner would would require that each somepage actually exists as somepage.php on the filesystem.
With the right combination of regular expressions this should be possible to some extent. However, I can foresee some possible issues with index pages not being requested correctly and not matching directories correctly.
For example, this will correctly rewrite example.com/test as a request for example.com/test.php:
RewriteEngine on
RewriteRule ^(.*)$ $1.php
But will make example.com fail to load because there is no example.com/.php
I'm going to guess that if you're removing all trailing slashes, then picking a request for a directory index from a request for a filename in the parent directory will become almost impossible. How do you determine a request for the directory 'foobar':
example.com/foobar
from a request for a file called foobar (which is actually foobar.php)
example.com/foobar
It might be possible if you used the RewriteBase directive. But if you do that then this problem gets way more complicated as you're going to require RewriteCond directives to do filesystem level checking if the request maps to a directory or a file.
That said, if you remove your requirement of removing all trailing slashes and instead force-add trailing slashes the "no .php extension" problem becomes a bit more reasonable.
# Turn on the rewrite engine
RewriteEngine on
# If the request doesn't end in .php (Case insensitive) continue processing rules
RewriteCond %{REQUEST_URI} !\.php$ [NC]
# If the request doesn't end in a slash continue processing the rules
RewriteCond %{REQUEST_URI} [^/]$
# Rewrite the request with a .php extension. L means this is the 'Last' rule
RewriteRule ^(.*)$ $1.php [L]
This still isn't perfect -- every request for a file still has .php appended to the request internally. A request for 'hi.txt' will put this in your error logs:
[Tue Oct 26 18:12:52 2010] [error] [client 71.61.190.56] script '/var/www/test.peopleareducks.com/rewrite/hi.txt.php' not found or unable to stat
But there is another option, set the DefaultType and DirectoryIndex directives like this:
DefaultType application/x-httpd-php
DirectoryIndex index.php index.html
Update 2013-11-14 - Fixed the above snippet to incorporate nicorellius's observation
Now requests for hi.txt (and anything else) are successful, requests to example.com/test will return the processed version of test.php, and index.php files will work again.
I must give credit where credit is due for this solution as I found it Michael J. Radwins Blog by searching Google for php no extension apache.
Remove trailing slashes
Some searching for apache remove trailing slashes brought me to some Search Engine Optimization pages. Apparently some Content Management Systems (Drupal in this case) will make content available with and without a trailing slash in URLs, which in the SEO world will cause your site to incur a duplicate content penalty. Source
The solution seems fairly trivial, using mod_rewrite we rewrite on the condition that the requested resource ends in a / and rewrite the URL by sending back the 301 Permanent Redirect HTTP header.
Here's his example which assumes your domain is blamcast.net and allows the the request to optionally be prefixed with www..
#get rid of trailing slashes
RewriteCond %{HTTP_HOST} ^(www.)?blamcast\.net$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
Now we're getting somewhere. Lets put it all together and see what it looks like.
Mandatory www., no .php, and no trailing slashes
This assumes the domain is foobar.example and it is running on the standard port 80.
# Process all files as PHP by default
DefaultType application/x-httpd-php
# Fix sub-directory requests by allowing 'index' as a DirectoryIndex value
DirectoryIndex index index.html
# Force the domain to load with the www subdomain prefix
# If the request doesn't start with www...
RewriteCond %{HTTP_HOST} !^www\.foobar\.com [NC]
# And the site name isn't empty
RewriteCond %{HTTP_HOST} !^$
# Finally rewrite the request: end of rules, don't escape the output, and force a 301 redirect
RewriteRule ^/?(.*) http://www.foobar.example/$1 [L,R,NE]
#get rid of trailing slashes
RewriteCond %{HTTP_HOST} ^(www.)?foobar\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
The 'R' flag is described in the RewriteRule directive section. Snippet:
redirect|R [=code] (force redirect) Prefix Substitution with
http://thishost[:thisport]/ (which makes the new URL a URI) to force
a external redirection. If no code is given, a HTTP response of 302
(MOVED TEMPORARILY) will be returned.
Final Note
I wasn't able to get the slash removal to work successfully. The redirect ended up giving me infinite redirect loops. After reading the original solution closer I get the impression that the example above works for them because of how their Drupal installation is configured. He mentions specifically:
On a normal Drupal site, with clean URLs enabled, these two addresses
are basically interchangeable
In reference to URLs ending with and without a slash. Furthermore,
Drupal uses a file called .htaccess to tell your web server how to
handle URLs. This is the same file that enables Drupal's clean URL
magic. By adding a simple redirect command to the beginning of your
.htaccess file, you can force the server to automatically remove any
trailing slashes.
In addition to other answers above,
You may also try this to remove .php extensions completely from your file and to avoid infinite loop:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
This code will work in Root/.htaccess,
Be sure to change the RewriteBase if you want to place this to a htaccess file in sub directory.
On Apache 2.4 and later, you can also use the END flag to prevent infinite loop error. The following example works same as the above on Apache 2.4,
RewriteEngine on
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
The following code works fine for me:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
After changing the parameter AllowOverride from None to All in /etc/apache2/apache2.conf (Debian 8), following this, the .htaccess file just must contain:
Options +MultiViews
AddHandler php5-script php
AddType text/html php
And it was enough to hide .php extension from files
I've ended up with the following working code:
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
Here's a method if you want to do it for just one specific file:
RewriteRule ^about$ about.php [L]
Ref: http://css-tricks.com/snippets/htaccess/remove-file-extention-from-urls/
Try this
The following code will definitely work
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
Not sure why the other answers didn't work for me but this code I found did:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
That is all that is in my htaccess and example.com/page shows example.com/page.php
To remove the .php extension from a PHP file for example yoursite.example/about.php to yoursite.example/about: Open .htaccess (create new one if not exists) file from root of your website, and add the following code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
To remove the .html extension from a HTML file for example yoursite.example/about.html to yoursite.example/about: Open .htaccess (create new one if not exists) file from root of your website, and add the following code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Reference: How to Remove PHP Extension from URL
Try this:-
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
I found 100% working Concept for me:
# Options is required by Many Hosting
Options +MultiViews
RewriteEngine on
# For .php & .html URL's:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Use this code in Root of your website .htaccess file like :
offline - wamp\www\YourWebDir
online - public_html/
If it doesn't work correct, then change the settings of your Wamp
Server: 1) Left click WAMP icon 2) Apache 3) Apache Modules 4) Left
click rewrite_module
Here is the code that I used to hide the .php extension from the filename:
## hide .php extension
# To redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L,NC]
Note: R=301 is for permanent redirect and is recommended to use for SEO purpose. However if one wants just a temporary redirect replace it with just R
Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
If you're coding in PHP and want to remove .php so you can have a URL like:
http://yourdomain.example/blah -> which points to /blah.php
This is all you need:
<IfModule mod_rewrite.c>
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
If your URL in PHP like http://yourdomain.example/demo.php than comes like
http://yourdomain.example/demo
This is all you need:
create file .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]

.htaccess rewrite all URLs, even if path

I've made some PHP code that will direct the user based on the URL, and I've found some code for htaccess that gives the URL to the index page which lets my code work. However, it's also technically revealing what is a file/folder and what isn't.
If I go to website.com/somethingrandom/somethingelse, I'll get a 404 error, whereas going to website.com/cache/private will say forbidden.
How would I edit it so that it will act like no files exist, aside from a few select file types, such as images, css, and js files?
Here is the code as it currently stands:
RewriteBase /
RewriteEngine On
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-f
# This rule converts your flat link to a query
RewriteRule ^(.*)$ index.php?_page_location=$1 [L,NC,NE]
Have your rules like this:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L,NC]
# add trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
# If the request is not for known file types
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|php|bmp|png|ico|tiff|css|js)$ [NC]
# This rule converts your flat link to a query
RewriteRule .* index.php?_page_location=$0 [L,QSA]
If you want to hide some file or directory, i.e. appear it as not existant, you might redirect with the R|redirect flag
RewriteRule ^cache/private - [R=404,L]
This tells the client, there is no such file named cache/private (404). Beware though, that this is for all clients, even yourself.
To restrict this, you must prefix the rule with some RewriteCond or wrap it in some conditional statement like If or similar.
If you want to hide all files, use a "broader" regular expression, like
RewriteRule ^ - [R=404,L]
This will return a "Not found" for all of your website's URLs. You can also give a set of URLs, like in this
RewriteRule ^(?:cache/private|secrets|database) - [R=404,L]
which will hide everything below
cache/private
secrets
database
See Apache - Regular Expressions and http://www.regular-expressions.info/ for details on how to customize your own pattern.

finding the names and view of all apache environment variables

I just want to know if there is a link with all of the environment variables in apache and what they look like when they are printed out.
The reason is i am trying to write some regex for .htacess mod_rewrite, but i don't know what these variables are printing. it's hard to write regex when im not sure what is printed, i keep getting them wrong.
Is there a list somewhere i am missing.
Trust me googling is easier than posting a question and waiting for response and people not quite sure what you are asked.
I can't seem to find a google source.
Eg %{THE_REQUEST} GET /index.php HTTP/1.1
The real problem i am having is i have this .htaccess file
# Do not remove this line, otherwise mod_rewrite rules will stop working
RewriteBase /
Options +Multiviews
AddHandler application/x-httpd-php .css
AddHandler application/x-httpd-php .js
Options +FollowSymLinks
RewriteEngine On
#NC not case sensitive
#L last rule don't process futher
#R 301 changes the url to what you want
RewriteCond %{HTTP_HOST} !^example\.host56\.com
RewriteRule ^(.*)$ http://example.host56.com/$1 [R=302,L]
RewriteRule ^demo(.*)$ finished$1 [NC]
RewriteCond %{REQUEST_URI} /
RewriteRule ^(.*)$ home/$1
I keep getting redirected to the error page
I am trying to get to
example.host56.com/home/
But it keeps leading me to errors. The home folder has a index.php file inside of it as well
Here's a mod_rewrite variable cheat sheet: http://www.askapache.com/htaccess/mod_rewrite-variables-cheatsheet.html
The rule here:
RewriteCond %{REQUEST_URI} /
RewriteRule ^(.*)$ home/$1
Is looping. The reason is because the %{REQUEST_URI} variable always starts with a /, and you're not using either the "^" or "$" to denote matching boundaries, so that condition will always be true. Since it's always true, the rule will always be replied. And since the rewrite engine continually loops until the URI stops changing (or until you've reached the internal recursion limit, causing a 500 error), the pattern always matches. Try changing it to:
RewriteCond %{REQUEST_URI} !^/home/
RewriteRule ^(.*)$ home/$1
or
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ home/$1

Making RewriteRule to mock reddit's or Rails?

For fun I want to make a reddit clone in php. I have mostly java experience but know my way around php, if that matters at all. I also have very limited experince with Rails 3 and I believe how rails deals with URLs is also close to how I want to handle it.
So far in a .htaccess I have:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php/$1 [L]
This routes every page to index.php where I parse the URI and handle requests with a switch. I know this isn't scalable in the long run but I want to get off the ground.
My next stepis to add a / automaticly at the end of a URL like localhost/r/all so it becomose localhost/r/all/ I want and have tried to make a RewriteRule like:
RewriteRule ^/?(.+)$ /$1/ [R]
This gives a 500 error. I think it is because it gets stuck in an infinite loop of redirect because /$1/ will equal ^/?(.+). So I need something like:
RewriteRule ^/?(.+)Does not end in a forwardlash$ /$1/ [R]
How can I do this? I see that the ! charecter can be used to denote a condition it should not match, but I am having trouble getting this to work.
Any Advice is appreciated.
EDIT:
getenv("DOCUMENT_ROOT") //gives /var/www
I have /var/www/index.php living, well in /www.
My .htaccess file which I am making these changes to lives in /var.
I have .htaccess in /var because in my httpd.cong I have this:
<Directory /var>
AllowOverride All
</Directory>
I first tried to have it as
<Directory /var/www>
and putting .htaccess in www but it is never read. I know it is not read becuase I put garbage in it and there is no error. But, if I have garbage in it when it is in /var and the Drectory directive specifies /var I get an error.
Try this (untested):
# Turn mod_rewrite on, yada yada yada
RewriteEngine On
RewriteBase /
# We'll want to do the redirect before rewriting anything
# Files obviously don't have a trailing slash, and directories require one - so
# we only need to check if the file exists
# I'm not actually sure if mod_rewrite understands non-capturing groups though
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?((?:.+?)[^/])$ http://%{HTTP_HOST}/$1/ [L,R=301]
# Do the rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php/$1 [L]
I get the feeling that the problem with your attempt is likely to be the order of the rules, a missing RewriteCond on the redirect RewriteRule and/or that you didn't put a L flag in the redirect RewriteRule - but the above regexes make me happier than yours do, because they are a little more specific about what they match.
I'm not actually sure if mod_rewrite supports all the regex features I have used above, so if you continue to get an error that might be why - come back here if you do have a problem and I'll look at it more closely.

HTACCESS REQUEST_URI without the filename

RewriteEngine On
RewriteCond %{REQUEST_URI} !(images)
RewriteRule ^(.*)\.(txt|rtf|docx?|odt|pdf|bmp|gif|jpe?g|png|tiff?)$ http://%{SERVER_NAME}/~abc123/uploader/process.php [nc]
Is there any way of not having to quote ~abc123/uploader but to modify my regular expression so that the request_uri without the filename is dynamically passed through?
I've tried looking at removing the filename from the request_uri and also changing my regular expression but to no avail.
What I'm trying to do is to make sure certain file types are processed by a PHP script and cannot be accessed directly.
Here are two clues that might help you:
First, you can use those keywords into your regular expression (I let you google for more information) : %{SCRIPT_FILENAME} and %{REQUEST_URI}
Second, here's an example of how to use it:
# (1) if domain name is static:
RewriteCond %{HTTP_HOST} ^(.*)\.s\.(.*) [NC,OR]
RewriteCond %{HTTP_HOST} ^(.*)\.static\.(.*) [NC]
# (2) and it's not the JavaScript directory:
RewriteCond %{REQUEST_URI} !^/js/(.*)$
# (3) *always* add /templates/ (if not there):
RewriteRule /(templates/)*(.*) /templates/$2 [L]
And a few people know that you can even change the whole destination filename this way (note: the STATIC and PATH_LOCAL variable is an environment variable that I've calculated a few steps before):
# If static...
RewriteCond %{ENV:STATIC} 1
# ...first test to see if the file exists in the language path:
RewriteCond %{DOCUMENT_ROOT}/%{ENV:PATH_LOCAL}/%{ENV:EXT}%{REQUEST_FILENAME} -f
# It exists => rewrite filename then end:
RewriteRule ^(.+) %{DOCUMENT_ROOT}/%{ENV:PATH_LOCAL}/%{ENV:EXT}%{REQUEST_FILENAME} [QSA,L]
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(?!images)(.*)/[-\w\.]*$
RewriteRule ^(.*)\.(txt|rtf|docx?|odt|pdf|bmp|gif|jpe?g|png|tiff?)$ http://%{SERVER_NAME}/%1/process.php [NC,L]
will do what you ask. The (?!...) is a negative lookahead i.e don't include images. The [-\w.]*$ bit will match normal file names. What I don't understand is that a http://%{SERVER_NAME}/... redirect without the [R] is just a normal internal rewrite why you don't just do
RewriteRule ^(?!images)(.*)/[-\w]*\.(?:txt|rtf|docx?|odt|pdf|bmp|gif|jpe?g|png|tiff?)$ $1/process.php [NC,L]
Without any conds or
RewriteCond %{REQUEST_URI} !^images
RewriteCond %{REQUEST_URI} (txt|rtf|docx?|odt|pdf|bmp|gif|jpe?g|png|tiff?)$
RewriteRule ^(.*)/.* $1/process.php [NC,L]
if you want to keep it easier to understand.

Categories