Need help with .htaccess file

Hello!
I need all links ending in a digit to get to http://mysite.com/test.php?/d/get/thelink,

for example http://mysite.com/fsdfsf8 should get to http://mysite.com/test.php?/d/get/fsdfsf8.

I also need al links not ending with a digit to get to http://mysite.com/test.php?/thelink

for example http://mysite.com/hello to get to http://mysite.com/test.php?/hello and http://mysite.com/hello/no to get to http://mysite.com/test.php?/hello/no .

I’ve tried several different combinations but I havn’t been able to solve it… here is what I tested last:


RewriteEngine on
RewriteBase /
RewriteRule ([7-8]+) index.php?/files/get/$1 
RewriteRule (.*) index.php?/$1   

What am I doing wrong? Thanks :slight_smile:

You need a little more to get it to work - like before the digit you’ll need to actually catch any other character, and using the $ to signify its at the end, and not just a numeric anywhere.

To also make sure you stop processing further rules after that has happened, you need the option L (last)


RewriteEngine on
RewriteBase /
RewriteRule (.+[0-9])$ index.php?/files/get/$1 [L,R=301]
RewriteRule (.+) index.php?/$1 [L,R=301]

Thanks! However I didn’t get that working so decided to do it this way instead and this almost works :stuck_out_tongue:

RewriteRule ^nd(.*) /index.php$1  [L]
RewriteRule ^([a-zA-Z0-9---_]+/?[a-zA-Z0-9---_]+?)$ /index.php/file/go/$1 [L] 

all links beginning with http://mydomain.com/nd follows the first rule and all other links should follow the second rule.

This works as long as there are not dots in the second rule, so what I need to find out is how to be able to have links with dots in them… for example:
http://mysite.com/adfdfs works
http://mysite.com/adfdfs/dfdf works
http://mysite.com/adfdfs/dfdf.add does not work but shoud work.

You need to add . into the list of valid characters, because . is a wildcard, you’ll need to escape it \.

Okey, great, thanks :slight_smile:

Now I’ve gotten this far:


RewriteRule ^deletefile/(.*) test.php/test/delete/$1    [L]
RewriteRule (.*[qQ]/?[a-zA-Z0-9\\.\\(\\)=?\\}\\]\\[\\{-]*?)$ test.php?/test/get/$1  [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\\.php|images|img|css|js|robots\\.txt)
RewriteRule (.*) test.php?/$1 [L]

It works apart from one thing, any URL looking like this: http://mysite.com/go/view/sdfsdsdfsfQ gets to test.php/test/get/sdfsdsdfsfQ when it should go to test.php/go/view/sdfsdsdfsfQ… It follows the first rule even though go doesn’t end with a Q or q, just the last peace of the link.

So I need to somehow limit the first rule to just apply when the link lools like http://mysite.com/XXXXXq or http://mysite.com/XXXXXq/somethnig but not apply when the link looks like http://mysite.com/something/XXXXXq or http://mysite.com/something/XXXXXq/test

Since these rewrite rules are going to grab whoever they can match first and go from there, I’d catch the
mysite.com/something’s first. If “something” never ever has q or Q in it, then I’d say something like
^([^qQ]+) first thing after domain is one or more of anything that’s not q or Q. Or if it wasn’t supposed to end with q/Q: ^(.+[^qQ]$)

RewriteRule [b]^([^qQ]+)[/b]/?[a-zA-Z0-9\\.\\(\\)=?\\}\\]\\[\\{-]*?)$ test.php?/go/view/$1  [L]

RewriteRule (.*[qQ]/?[a-zA-Z0-9\\.\\(\\)=?\\}\\]\\[\\{-]*?)$ test.php?/test/get/$1  [L]

I don’t pretend to know what all the stuff at the end of your regex is… you really have all those things in your URLs?

The problem is the :kaioken: .* :kaioken: in front of the [qQ] that will eat up EVERYTHING AND ANYTHING, including slashes!!

From what you’re saying it sounds like you want to redirect to /test/get if and only if the last character of the first “directory” is a either or a “q” or a “Q”.

So, you’d first want to match anything that is not a slash, to make sure you remain in the first directory, and that needs to end with either a “q” or a “Q”.

In other words:


RewriteRule ([^/]*[qQ]/?[a-zA-Z0-9]\\.\\(\\)=?\\}\\]\\[\\{-]*?)$ test.php?/test/get/$1  [L]

I agree with Stomme that the right part of that expression looks utterly weird. It seems you want to match the query string here, but you can’t do that in a RewriteRule. Only RewriteCond’s can match the query string. Get rid of it.


RewriteRule ^[^/]*[qQ]/? test.php?/test/get/$1  [L]

Haha, you’ve picked up the Flaming 'Splodeheads : )

It works now, the solution is: RewriteRule ^([^/]*[qQ]/?.+?)$ Thanks!

Okay, Oskare, sorry for my delay in entering into this fray!

Please do four things before going any further:

  1. SCAN the Berners-Lee for allowed characters in a URI.

  2. READ (at least the regex development section) of the mod_rewrite tutorial linked in my signature - the coding samples may also benefit you, too.

  3. Drop back to the start and create a specification for yourself similar to the way that you started. Then LISTEN to the specification to understand where you’re specifying that mod_rewrite should LOOP. When you find these points, then you’ll know to specify exclusions which will become your RewriteCond modifiers of your RewriteRule(s).

  4. MultiViews? IMHO they’re to be avoided but, if you insist, then it looks like you’ll be fine (having whatever.php parse the path for the fields you need for your script).

Regards,

DK