View Full Version : Need help with .htaccess file

12-04-2010, 09:09 PM
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 :)

12-05-2010, 05:21 AM
RewriteRule (.*[0-9])$ index.php?/files/get/$1 [L]
RewriteRule (.*) index.php?/$1

That should do it. Note the [L] to stop it trying any more matches, and the $ to anchor the regex to the end of the url

12-05-2010, 03:05 PM
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.

12-05-2010, 06:25 PM


Anything, ending with a q or Q, optional /, 0 or many (non-greedy) range of letters. I'm not sure why you modified the * with ? as it's anchored anyway. You need to make the / or final range of characters have something not optional. It does match "/go/view/sdfsdsdfsfQ". Maybe if you're more specific about what *exactly* you need it to do, I can help more.

12-05-2010, 08:39 PM
Oh, ok, well, absolutely, I've never used the .htaccess file before so I 'm not sure about what exactly everything means. I found a guide and in an example they used ? so I tested that as well :P

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

The script I'm working on is based on another script and has different so called member pages, all of those looks like this: http://mysite.com/XXXXXQ, http://mysite.com/XXXXXq, http://mysite.com/XXXXXQ/notimportant or http://mysite.com/XXXXXq/notimportant - all of those should get to for example http://mysite.com/test.php/test/get/XXXXXq or http://mysite.com/test.php/test/get/XXXXXq/notimportant.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(test\.php|images|img|css|js|robots\.txt)

That code has something to do with the base script, don't know exactly what it does but if I remove that all the fixed links stop working.

RewriteRule (.*) test.php/$1 [L]

Then from the base script there are links that looks like http://mysite.com/view/download/XXXXXq and http://mysite.com/view/download/XXXXXQ and those should get to http://mysite.com/test.php/view/download/XXXXXQ plus other fixed links as for example http://mysite.com/view that should get to http://mysite.com/test.php/view

So I think 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

12-05-2010, 11:46 PM
I find it's best to be quite specific, I'd try:

# For /XXXXQ or /XXXXq only, with optional trailing slash
RewriteRule ^(.*[qQ]/?)$ /test.php.... [L]

# For /XXXXQ/something or /XXXXq/something
RewriteRule ^(.*[qQ]/.+)$ /test.php.... [L]
# If you want that to not include any paths past "something", do:
# RewriteRule ^(.*[qQ]/[^/]+/?)$ /test.php.... [L]
# ... which won't work with XXXXq/something/more

# Everything else
RewriteRule ^(.*)$ /test.php....

Replace .... with whatever page it's meant to be, frankly I got a bit confused :D

12-06-2010, 11:25 AM
Great, that seams to almost be the solution :D I'm very thankful for the help you have given and I'm starting to learn how these files work now :) However, I see now that I unfortunately gave you wrong information on how the second links looks like (really sorry for that)...

This one:
RewriteRule ^(.*[qQ]/[^/]+/?)$ ......$1

I think doesn't take like

which is exactly what I wrote, but I ment that it shouldn't take links like

which are now taken by
RewriteRule ^(.*[qQ]/?)$ .....$1

So I need to somehow limit so the link only applies to the rules if it starts with XXXXXq (like http://mysite.com/XXXXXq or http://mysite.com/XXXXq/test), and not if XXXXXq is somewhere else in the link (like http://mysite.com/thisis/XXXXXq.


12-06-2010, 11:27 AM
Did you put the [L] after "RewriteRule ^(.*[qQ]/?)$ .....$1", and ensure it appears before other entries?

12-06-2010, 04:25 PM
Yes, this is how the lines look like now:

RewriteRule ^(.*[qQ]/?)$ test.php?/test/get/$1 [L]
RewriteRule ^(.*[qQ]/[^/]+/?)$ test.php?/test/get/$1 [L]
RewriteRule ^(.*) index.php?/$1 [L]

So links like http://mysite.com/test/test/XXXXQ should follow the third rule but now it seams like it follows the first or second rule... Have I missplaced anything? Thanks :)

12-06-2010, 05:25 PM
For clarity, can you give a list in the following format:

Destination: /index.php?/$1

Destination: /test.php?/test/get/$1

and any other destinations

12-06-2010, 07:13 PM

Destination: /index.php?/$1

Destination: /test.php?/test/get/$1

12-06-2010, 07:26 PM
# /XXXXQ/something
RewriteRule ^(/[^/]*[Qq])/?$ /test.php?/test/get/$1 [L]
RewriteRule ^(/[^/]*[Qq])/.+)/?$ /test.php?/test/get/$1 [L]

# /something/XXXXQ
RewriteRule ^(/.*/[^/]*[Qq])/?$ /index.php?/$1 [L]

12-06-2010, 07:40 PM
Hmm.. this one:

RewriteRule ^(/[^/]*[Qq])/?$ /test.php?/test/get/$1 [L]

does not work with

And this one:

RewriteRule ^(/[^/]*[Qq])/.*)/?$ /test.php?/test/get/$1 [L]

gives me

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

and if I remove the extra ) it doesn't give me Internal Server Error anymore but it doesn't do what it should anyway..


12-06-2010, 07:54 PM
Sorry about that extra )

RewriteRule ^(/[^/]*[Qq])/?$ /test.php?/test/get/$1 [L]

# /XXXXQ/something
RewriteRule ^(/[^/]*[Qq]/.+)/?$ /test.php?/test/get/$1 [L]

# /something/XXXXQ
RewriteRule ^(/.*/[^/]*[Qq])/?$ /index.php?/$1 [L]

12-06-2010, 08:05 PM
Also, I'm not an expert at Apache config. I am an expert at Regex (forgetting the odd extra parenthesis). I wonder what'll happen if you remove the RewriteCond and RewriteBase lines...

12-06-2010, 08:21 PM
And if you add them together: RewriteRule ^([^/]*[qQ]/?.+?)$.. It works :D Thanks!!

08-13-2011, 12:56 PM
Some good examples can be found here: http://www.evolt.org/ultimate_htaccess_examples