Helpful Information
 
 
Category: Regex Programming
Most not start with must end with

Hi I'm looking for a mod rewrite pattern that will redirect a user to /testdir/ if the url doesn't start with /start/here/ and ends with .php
Also each directory in the url must go after /testdir/
Examples
URL: /user/one/two/three.php
Redirect: /testdir/user/one/two/three

URL : /start/here/one/two/three.php
Redirect :false no redirect

This is what I have so far:

^(^(\/start\/here\/\))(.*).php$ /testdir/$1 [R=301,QSA,L]

This doesn't work
Can anyone suggest anything?

RewriteRule ^/?((?!start/here).*|.*(?<!\.php)) /testdir/$1 [R=301,QSA,L]
Try that.

[edit] I interpreted what you said to mean there should be a redirection if it doesn't start with "start/here" OR the request doesn't end with ".php"

RewriteRule ^/?((?!start/here).*|.*(?<!\.php)) /testdir/$1 [R=301,QSA,L]
Try that.

[edit] I interpreted what you said to mean there should be a redirection if it doesn't start with "start/here" OR the request doesn't end with ".php"
Sorry i meant it doesn't start with start/here but it needs to end with .php
My latest is try is:
RewriteRule ^/?((?!start/here).*(\.php$)) /testdir/$1 [R=301,QSA,L]

I want to redirect any url which doesn't start with /start/here/ but end with .htm
I also need to add anything inbetween / and .htm to /testdir

My above example redirects from
/test/test/x.htm
to /testdir/testdir/testdir/x.htm
It warns of a redirect loop and crashes out

i need it to redirect to
/testdir/test/x/

How about

RewriteRule ^/?(?!testdir|start/here)(.*\.(php|html?))$ /testdir/$1 [R=301,QSA,L]
If it doesn't start with start/here and it's a PHP or HTML file (added the optional 'l' just to be safe) it's redirected to testdir.

I have it working:

RewriteRule ^/?(?!start/here)(.*)(\.php$) /testdir/$1 [R=301,QSA,L]
A condition checks if the url doesn't start with start/here
It places all the bits after that to $1 and checks that it ends with .php
and then it all hte bits is $1 after /testdir/
Yippee I have it working










privacy (GDPR)