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
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
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.
TimIgoe
December 5, 2010, 10:39am
4
You need to add . into the list of valid characters, because . is a wildcard, you’ll need to escape it \.
Okey, great, thanks
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.
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?
rpkamp
December 6, 2010, 11:58am
8
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!
dklynn
December 6, 2010, 8:28pm
11
Oskare100:
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 .
That’s simple enough.
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 .
That’s LOOPY - test.php is part of your “all” as there are no exclusions, i.e., test.php and all your css, js, images, yadda-yadda." Having stated your “specification” this way, this should be obvious (to a human regex parser).
I’ve tried several different combinations but I havn’t been able to solve it… here is what I tested last:
RewriteEngine on
[COLOR="Red"]RewriteBase /[/COLOR]
[COLOR="Red"]# What Redirect is this UNdoing? Remove it![/COLOR]
RewriteRule ([7-8]+) index.php?/files/get/$1
[COLOR="Red"]# only matches 7 or 8 ANYWHERE in the {REQUEST_URI} string
# and creates an improperly formatted query string[/COLOR]
RewriteRule (.*) index.php?/$1
[COLOR="Red"]# this FURTHER redirects :kaioken: EVERYTHING :kaioken: to index.php
# (including index.php) overwriting any query string
# with another improperly formatted query string (/index.php)[/COLOR]
What am I doing wrong? Thanks
[indent][COLOR=“Blue”]Mostly, just not listening to your own specification of the problem.
Also, are you redirecting to test.php or index.php?[/COLOR][/indent]
TimIgoe:
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)
[indent]True … to a point. It directss mod_rewrite to RESTART processing with the new {REQUEST_URI} string.
RewriteEngine on
[COLOR="Red"]RewriteBase /[/COLOR]
RewriteRule (.+[0-9])$ index.php?/files/get/$1 [L,R=301]
[COLOR="Red"]# Good - prevents the loop[/COLOR]
RewriteRule (.+) index.php?/$1 [L,R=301]
[COLOR="Red"]# :nono: Same loopy problem, same loopy result![/COLOR]
Oskare100:
Thanks! However I didn’t get that working so decided to do it this way instead and this almost works
Not even close.
RewriteRule ^nd(.*) /index.php$1 [L]
RewriteRule ^([a-zA-Z0-9[COLOR="Red"]---_[/COLOR]]+/?[a-zA-Z0-9[COLOR="Red"]---_[/COLOR]]+?)$ /index.php/file/go/$1 [L]
[indent][COLOR="Blue"]You've GOT to be kidding, right?
What in the world is ---_ supposed to be doing?
Do you have Options MultiViews operating (to allow index.php to be served when it's in the path? MultiViews is more trouble than it's worth![/COLOR][/indent]
all links beginning with http://mydomain.com/nd follows the first rule and all other links should follow the second rule.
That’s on the first pass. What happens on the second (and subsequent pass through your mod_rewrite code which it does UNTIL there are no more matches?
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.
That’s a simple matter of learning some regex.
Oskare100:
Okey, great, thanks
Now I’ve gotten this far:
What happened to the original, loopy “specification”?
RewriteRule ^deletefile/(.*) test.php/test/delete/$1 [L]
RewriteRule (.*[qQ]/?[a-zA-Z0-9\\.\\(\\)=?\\}\\]\\[\\{-]*?)$ test.php?/test/get/$1 [L]
[indent][COLOR="Red"]YGBSM! Please have a look at Tim Berners-Lee's techno-treatise on the syntax (allowed/reserved/etc characters allowed in a [URI](http://www.ietf.org/rfc/rfc2396.txt)).[/COLOR][/indent]
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.
Stomme_poes:
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?
Good pick-up, Mallory. Since those characters are ILLEGAL in a URI, the obvious answer is a resounding NO!
rpkamp:
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]
Rémon, read through again as this has been a total mess from the start.
Okay, Oskare, sorry for my delay in entering into this fray!
Please do four things before going any further:
SCAN the Berners-Lee for allowed characters in a URI.
READ (at least the regex development section) of the mod_rewrite tutorial linked in my signature - the coding samples may also benefit you, too.
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).
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