#1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2005
    Location
    UK
    Posts
    556
    Rep Power
    82

    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:
    Code:
    ^(^(\/start\/here\/\))(.*).php$ /testdir/$1 [R=301,QSA,L]
    This doesn't work
    Can anyone suggest anything?
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,056
    Rep Power
    9398
    Code:
    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"
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2005
    Location
    UK
    Posts
    556
    Rep Power
    82
    Originally Posted by requinix
    Code:
    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/
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,056
    Rep Power
    9398
    How about
    Code:
    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.
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2005
    Location
    UK
    Posts
    556
    Rep Power
    82
    I have it working:
    Code:
    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

IMN logo majestic logo threadwatch logo seochat tools logo