Forum Moderators: phranque

Message Too Old, No Replies

rewrite rule syntax help please

         

popzzz

1:48 am on Feb 29, 2008 (gmt 0)

10+ Year Member



I have reviewed all the requisite files such as:
An Introduction to Redirecting URLs on an Apache Server
Mod_Rewrite Beginning
Mod_Rewrite and Regular Expressions
etc, and a lot of the messages and have not found a solution so thought I would try for some help with a post.

I am trying to rewrite:
mydomain.com/a/variable
into
mydomain.com/a/index.php?variable (works)
and have tried it seems like every combination there is but nothing so far works.

My best guess effort with .htaccess in the /a/ directory is:
RewriteEngine on
RewriteRule ^(.*) /index.php?a=$1 [L]

I am certainly not very good with regex (obvious) but need a little help before I run out of hair to pull out. LOL.

Thanks in advance for any welcomed help or solution,
popzzz

jdMorgan

2:30 am on Feb 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's nothing obvious wrong with what you've got, assuming that your code is in example.com/a/.htaccess and your index.php script is located at /example.com/index.php

So what specifically is (or is not) happening that leads you to post here?
How did you test?
What were the results?
How did they differ from your expectations?
(No such thing as too many details, here) :)

And do remember to completely flush your browser cache before testing any new server-side code.

Jim

popzzz

3:35 am on Feb 29, 2008 (gmt 0)

10+ Year Member



Hi, (and thanks)

The code is in example.com/a/.htaccess and the index.php script is located at /example.com/a/index.php.

The script works fine when accessed directly via example.com/a/index.php?a=variable using any variables that I put in the script.

I have tried all of the following with no success and more I’m sure:
RewriteRule ^(.*) index.php?a=$1 [L]
RewriteRule ^(.*) /index.php?a=$1 [L]
RewriteRule ^(.*) /a/index.php?a=$1 [L]
RewriteRule ^/(.*) index.php?a=$1 [L]
RewriteRule ^/(.*) /index.php?a=$1 [L]
RewriteRule ^/(.*) /a/index.php?a=$1 [L]
RewriteRule ^/a/(.*) index.php?a=$1 [L]
RewriteRule ^/a/(.*) /index.php?a=$1 [L
RewriteRule ^/a/(.*) /a/index.php?a=$1 [L]
although I would like this to be internal, (silent) I have tried the following with same results:
RewriteRule ^(.*) http://example.com/a/index.php?a=$1 [L]
RewriteRule ^/(.*) http://example.com/a/index.php?a=$1 [L]
RewriteRule ^/a/(.*) http://example.com/a/index.php?a=$1 [L]

It always returns a 404 not found and the same in the error log.

One of them (don't remember which) loops and just keeps trying to reload the same url. (example.com/a/variable)

One of them (don't remember which) returns a 500 error.

That's all I can think of.

It seems so simple but I’m obviously missing something.

Thanks for the help,
popzzz

jdMorgan

4:25 am on Feb 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> The code is in example.com/a/.htaccess and the index.php script is located at /example.com/a/index.php.

In that case, your third trial rule is most correct, except that you must explicitly prevent an 'infinite' rewriting loop:


RewriteCond $1 !^index\.php$
RewriteRule (.*) /a/index.php?a=$1 [L]

If you still have a 404 problem, you'll need to 'stare hard' at the filepath shown in your error log, as you may have a path problem. If so, see the RewriteBase directive -- but don't use it unless you must.

Jim

popzzz

5:15 am on Feb 29, 2008 (gmt 0)

10+ Year Member



Thanks Jim … that's perfect!

Without the rewritecond (which I didn't have) that was the one that got the 500 error.

However WITH the rewritecond it works perfectly.

I need to study up more on the rewritecond, etc, as I have had some similar cases I have struggled with in the past.

Once again, thanks!
popzzz

popzzz

10:08 am on Mar 9, 2008 (gmt 0)

10+ Year Member



I spoke too soon I'm afraid.

It works perfectly with Firefox, however, with MS internet explorer it does the infinite loop, loading over and over.

I'm at a loss. Any further solution please?

Thank you in advance,
popzzz

popzzz

8:36 pm on Mar 10, 2008 (gmt 0)

10+ Year Member



Anyone have any ideas?

Thank you in advance,
popzzz

etdebruin

8:48 pm on Mar 10, 2008 (gmt 0)

10+ Year Member



Is it possible that the (.*) is too vague?

How about:

RewriteRule ^a/([a-z]+)/?$ /a/index.php?=$1 [NC,L]

This obviously assumes that your variable is a letter, but can be expanded to alpha numeric and others quite easily.

g1smd

9:24 pm on Mar 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Clear the cache on both browsers and try again.

popzzz

10:27 pm on Mar 10, 2008 (gmt 0)

10+ Year Member



etdebruin,

That stopped the reloading over and over but now it's a 404 not found with FF and IE both.

The log file shows the correct path as Jim suggested to check but now it is not finding the /a/variable which (I think) would indicate the rule is not quite right on the rewrite.

g1smd,

The caches are cleared with every new change.

Thanks for the help to both of you.
popzzz

etdebruin

10:53 pm on Mar 10, 2008 (gmt 0)

10+ Year Member



Does /a/index.php exist?

popzzz

11:11 pm on Mar 10, 2008 (gmt 0)

10+ Year Member



Yes /a/index.php exists and was working as stated above until this last change with the 404 not found.

Thanks again,
popzzz

etdebruin

11:37 pm on Mar 10, 2008 (gmt 0)

10+ Year Member



What is the exact possible syntax for 'variable'? Letters, numbers, special characters?

popzzz

11:56 pm on Mar 10, 2008 (gmt 0)

10+ Year Member



At the moment for testing I am only using alpha to conform with your example rewrite code as in /a/variable.

Eventually, when working it will be alpha numerical as in /a/variable001, etc, with no special characters.

I thought it best to get it solved before adding more confusion.

As an aside, the code you kindly provided:
RewriteRule ^a/([a-z]+)/?$ /a/index.php?=$1 [NC,L]
was immediately changed to:
RewriteRule ^a/([a-z]+)/?$ /a/index.php?a=$1 [NC,L]
as it was missing the necessary 'a'.

Thanks,
popzzz

etdebruin

12:10 am on Mar 11, 2008 (gmt 0)

10+ Year Member



Yes, you changed it correctly. Please note that my example assumes that your .htaccess is in the document root. Did you move it there?

popzzz

12:16 am on Mar 11, 2008 (gmt 0)

10+ Year Member



No I didn't move it as I need it in the /a/ directory specifically.

The code is in example.com/a/.htaccess and the index.php script is located at /example.com/a/index.php.

Thanks again,
popzzz

etdebruin

12:38 am on Mar 11, 2008 (gmt 0)

10+ Year Member



Hmm, have you heard about rewriteBase? Are you using that?

[httpd.apache.org...]

popzzz

1:14 am on Mar 11, 2008 (gmt 0)

10+ Year Member



Jim said earlier:
If you still have a 404 problem, you'll need to 'stare hard' at the filepath shown in your error log, as you may have a path problem. If so, see the RewriteBase directive -- but don't use it unless you must.

I have a 404 problem but it doesn't seem to be a 'path' problem as the request seems to have the 'proper' path in the log:
/a/variable 404.

To me this indicates the rewrite code is not getting the variable to the /a/index.php to be processed.
If properly processed the log shows:
/a/variable 302.

Or am I wrong?

If I were to use it, would I write the statement as:
RewriteBase /a/

Thanks,
popzzz

etdebruin

1:38 am on Mar 11, 2008 (gmt 0)

10+ Year Member



Are you sure you don't have a folder called 'a' in document root?

Yes, you would use

rewriteBase /a

popzzz

1:39 am on Mar 11, 2008 (gmt 0)

10+ Year Member



Ok, I changed the code to:
RewriteRule ^([a-z]+)/?$ /b/index.php?a=$1 [NC,L]
and back to square one ....
works perfectly with FF and reloads over and over with IE.

Added RewriteBase /a/ with exactly the same results.

So it appears that the "RewriteBase" has no effect either way.

Thanks,
popzzz

popzzz

1:45 am on Mar 11, 2008 (gmt 0)

10+ Year Member



Are you sure you don't have a folder called 'a' in document root?

Yes.
/a/ is where all this is supposed to happen.

popzzz

1:50 am on Mar 11, 2008 (gmt 0)

10+ Year Member




RewriteRule ^([a-z]+)/?$ /b/index.php?a=$1 [NC,L]

Sorry, should have been:
RewriteRule ^([a-z]+)/?$ /a/index.php?a=$1 [NC,L]

etdebruin

2:02 am on Mar 11, 2008 (gmt 0)

10+ Year Member



How about pasting your whole .htaccess file in here?

popzzz

2:12 am on Mar 11, 2008 (gmt 0)

10+ Year Member



Contents of example.com/a/.htaccess

RewriteEngine on
RewriteBase /a/
RewriteCond $1 !^index\.php$
#RewriteRule (.*) /a/index.php?a=$1 [L]
RewriteRule ^([a-z]+)/?$ /a/index.php?a=$1 [NC,L]

etdebruin

2:27 am on Mar 11, 2008 (gmt 0)

10+ Year Member



I'm afraid that looks perfect. Not sure what to say other than some server configuration misstep. Sorry I could not help.

jdMorgan

3:17 pm on Mar 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is no redirect in that code, so the difference in behaviour between FF and IE cannot be explained, unless one of two conditions is met:

1) The IE browser cache has not been completely flushed.
2) A client-side agent (such as JavaScript or a cookie) is interacting with your server in a manner which differs from its behaviour with Firefox.

I don't use RewriteBase unless absolutely required, and prefer to put the entire path into the rules so that the RewriteBase for one rule does not affect other rules, but I doubt that that is the problem here.

From the start of this thread, there has been something very strange going on, and I'd suggest you contact your host, try this code on a different server, or change servers.

Jim

popzzz

4:01 pm on Mar 11, 2008 (gmt 0)

10+ Year Member



Hi again and thanks to everyone for the help.

After going around with the .htaccess, I now think the problem may be with the php
script itself at this thread that I started on the PHP board, "Looping Problem on Redirects in IE" [webmasterworld.com].

Thanks again,
popzzz

popzzz

4:26 am on Mar 13, 2008 (gmt 0)

10+ Year Member



RewriteRule ^a/([a-z]+)/?$ /a/index.php?=$1 [NC,L]
This obviously assumes that your variable is a letter, but can be expanded to alpha numeric and others quite easily.

Hi again all.

I got the script problem nailed if someone would be so kind as to help me write this rule for alpha numeric .
i.e. example.com/variable123

Thank you in advance,
popzzz

jdMorgan

4:29 pm on Mar 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please review the concise regular-expressions tutorial cited in our Forum Charter.

Jim