Forum Moderators: phranque
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
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
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
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]
Jim
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
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
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
[httpd.apache.org...]
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.
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
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
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
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