views:

97

answers:

4

Hello folks,

my boss wants me to code an URLrewriting for, let's say "http://our.domain.com/SomeText" -- that is, if the URL does NOT contain ".php" or ".html", it should be processed by a script like "http://our.domain.com/process.php?string=SomeText". Is this possible with a simple regex? I just cannot find the right expression to do this. I started by redirecting requests to some special subdir, but now it should work directly without any further subdir, so I somehow have to separate regular requests for exuisting pages to requests that don't contain ".php" or ".html"...

Any advice or link for me?

Best regards, Roman.

A: 

why you dont use mod rewrite ?

link to explain :

http://www.workingwith.me.uk/articles/scripting/mod_rewrite

Haim Evgi
+1  A: 

Something like this should do the trick

RewriteEngine on
RewriteCond %{REQUEST_URI} !\.php
RewriteCond %{REQUEST_URI} !\.html
RewriteRule (.*) /process.php?string=$1 [QSA]

there are some caveats regarding if this will go in .htaccess or directly in the VirtualHost definition (to put or not a leading /).

Additionally you should load mod_rewrite.

All this supposing you are actually using Apache and can use mod_rewrite.

What this does is that if the requested URI doesn't contain the strings .php and .html it then internally redirects the requests to the process.php file with what was written as a query string argument.

If you have problems with this then use

RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 9

and check how it is working. For this to work you have to control the server, as it has to be used in a main configuration file (no .htaccess possible).

Vinko Vrsalovic
A: 

At least with mod_rewrite you can use a regex which matches .php and .html, and then prepend that with a !, which should negate it.

Jani Hartikainen
A: 

Using mod_rewite you could do this to re-write .php and .html URLs like this:

RewriteEngine on
RewriteRule !\.(php|html)$ process.php/$1 [NC] [QSA]

However you may also want to stop it from re-writing URLs to files which actually exist. (There may be other files lying around - images/css etc. which you don't want to be re-written) You could amend the above to add checks:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(php|html)$ process.php/$1 [NC] [QSA]

Adding this check may mean that you don't actually need check whether the extension is anything other than .php or .html, because you know that it will only be rewritten if the requested file does not exist, so you could amend the RewriteRule line to be this:

RewriteRule ^(.*)$ process.php/$1 [NC] [QSA]

This will mean any requested URL that does not exist as a file will be handled by process.php.

Tom Haigh
Thank you a thousand times, also loads of hugs to everyone who answered to my question! This solution hits the spot and I'm glad I don't have to read the RegEx-bible from O'Reilly now.It works great!