Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite Redirect

         

methnen

7:38 am on Jun 22, 2005 (gmt 0)

10+ Year Member



I have a .htaccess file that currently has this in it:

RewriteEngine on
RewriteCond $1!^(favicon\.ico¦index\.php).*
RewriteRule ^(.*) index.php/jamie/$1

This portion of the .htaccess files is working perfectly. However there is one condition where the script I am using with these settings will throw something into the url that doesn't work. To be specific it will put the text

jamie

into the url which the script does not understand since I have the RewriteRule above that is putting

index.php/jamie/

into the url so the server is actually trying to visit

index.php/jamie/jamie/

which of course is goofy. I'm wondering if it would be possible to add some conditions and rules that would look to see if [mydomain...] is there and if it is to redirect to [mydomain...]

Anyone willing to help out a newbie? I will be incredibly grateful. I don't need anything passed to the script I just need it redirected so that the user doesn't know that the url was ever wrong in the first place.

Thanks a lot to anyone willing to take the time to help me out here. I greatly appreciate it.

Jamie

methnen

4:46 pm on Jun 22, 2005 (gmt 0)

10+ Year Member



Is this even possible?

BillyS

8:30 pm on Jun 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you only have one level at /jamie/? If so, this is possible.

methnen

9:03 pm on Jun 22, 2005 (gmt 0)

10+ Year Member



I'm assuming you mean is it only possible for /jamie/ to occur in one place? If so, then yes, it will only be possible for /jamie/ to occur immediately after the the domain name and there will never be another occurance of it in any other place.

Thanks for taking the time to help me out.

Jamie

BillyS

10:26 pm on Jun 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To be more specific...

Do you want to move everything down?

site.com/jamie/foo.html

to

site.com/foo.html

I think this should work:

RedirectMatch 301 /jamie/(.*) /$1

But this is where I come for expert help. Anyone?

methnen

10:35 pm on Jun 22, 2005 (gmt 0)

10+ Year Member



Yes I just want to get rid of the jamie part and move the rest of the content down. I will try your suggestion the moment I am home and have access to the server.

I'm definitely open to hearing the guru's take on how best to handle this as well. :)

Jamie

methnen

2:04 am on Jun 23, 2005 (gmt 0)

10+ Year Member



The above code seems to work for the redirect however it is causing some sort of loop and the browser reports that too many redirects occurred and it was unable to reload the page.

So this does not work:
RewriteEngine on
RewriteCond $1!^(favicon\.ico¦index\.php).*
RewriteRule ^(.*) index.php/jamie/$1
RedirectMatch 301 /jamie/(.*) /$1

Neither does this:
RewriteEngine on
RedirectMatch 301 /jamie/(.*) /$1
RewriteCond $1!^(favicon\.ico¦index\.php).*
RewriteRule ^(.*) index.php/jamie/$1

I'm suspecting I need some sort of rewrite condition and then skipping of the rule if the condition doesn't occur.

Any ideas?

Jamie

methnen

3:23 am on Jun 23, 2005 (gmt 0)

10+ Year Member



Stumbling along here as I mess with things.

I have a Rewrite Condition now that will check if the url already has /jamie/ in it. If it is in it then it skips the next rule. This looks like this:

RewriteCond!^/jamie/$ [S=1]

Now I need a Rule that does what the Rewrite Match you gave me did. Maybe I will be able to figure this one out on my own but I could certainly use some help.

Jamie

methnen

3:59 am on Jun 23, 2005 (gmt 0)

10+ Year Member



Still talking to myself. :)

Here is what I have now but it must not be formated right or something because it doesn't seem to be be getting rid of /jamie/

RewriteEngine on
RewriteCond!^/jamie/$ [S=1]
RewriteRule ^/jamie/(.*)$ [mydomain.com...] [R=301]
RewriteCond $1!^(favicon\.ico¦index\.php).*
RewriteRule ^(.*) index.php/jamie/$1 [L]

The third line is the one in question.

Anyone?

BillyS

1:28 pm on Jun 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



sorry methnen but that is about all I can do. The conference is ongoing so maybe many of the experts are attending...

methnen

2:16 pm on Jun 23, 2005 (gmt 0)

10+ Year Member



I didn't realize there was a conference going on. Apparently I chose a bad time to need help. :)

Jamie

jd01

7:51 pm on Jun 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Jamie,

This is your original rule:

RewriteEngine on
RewriteCond $1!^(favicon\.ico¦index\.php).*
RewriteRule ^(.*) index.php/jamie/$1

The only way that you are getting /jamie/jamie/ is if this ruleset is in the root and the directory jamie/ is being stored in the variable.

You have two options:
1. move the .htaccess file to the jamie/ directory, then the directory will not be stored by the varible, because it is the directory you are in and not used in comparrison.

2. change your rule to the specific directory of jamie/ and then use the catch all after that to store the pages. EG

RewriteRule ^jamie/(.*)$ /index.php/jamie/$1 [L]

Hope this helps.

Justin

Added: added a hard ending ($) to the left side of the rule, and a / to the right side... required in the .htaccess file, because the request is being sent back through the server. Ended the Rule with [L] last flag... should always be used unless you know you don't need it.

methnen

9:04 pm on Jun 23, 2005 (gmt 0)

10+ Year Member



Justin thanks for the advice.

I can't test what you just gave me until this evening but I wanted to mention one thing and see if this will cause a problem. The /jamie/ is not actually a directory it is actually a query string of sorts that the script uses to know which template_group i want the templates from the script to be loaded from. So there is not physical directory by that name. It is all in the mind of the script. That means I can't move the .htaccess file anywhere but root since there are no directories in root. It contains only the index.php a path.php file that the index.php refers to, a favicon.ico and the .htaccess file. There is nothing else physically in the directory.

How would this change what I need to do?

Jamie

methnen

10:06 pm on Jun 23, 2005 (gmt 0)

10+ Year Member



I wanted to mention too that most of the time the URL won't have /jamie/ in it so that the .htaccess file will still need to work in those situations. I'm hoping my needs aren't too specific for the abilities of .htaccess.

Jamie

methnen

11:05 pm on Jun 23, 2005 (gmt 0)

10+ Year Member



Also wanted to mention that the second jamie is most definitely a bug in the script I'm using so it is coming from the url that the script is attempting to use. What I am attempting to do is in the ONE case where it happens have some sort of rule that will remove that extra jamie so that script doesn't choke on its own mistake. Does this hopefully make sense?

jd01

11:48 pm on Jun 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteEngine on
RewriteRule ^jamie/jamie/?(.*)$ /jamie/$1 [R=301,L]

RewriteCond $1!^(favicon\.ico¦index\.php)
RewriteRule ^(.*)$ /index.php/jamie/$1 [L]

Rule 1: Send jamie/jamie (/ made optional by the use of ?) to jamie/ with a 301, external (visible) redirect.

Rule 2: store all file path info in a variable and make sure it is not a favicon.ico or index.php file.

Removed the .* from the condition. No point in determining a match if you do not need to store it as a variable or compare it to anything, so by leaving the line end ($) off, you can use the implicit 'and anything else'

Think this should get you close...

Sorry, I did not know exactly what you were asking the first time.

Justin

methnen

2:13 am on Jun 24, 2005 (gmt 0)

10+ Year Member



Justin,

I had to change the code to this:

RewriteEngine on
RewriteRule ^jamie/?(.*)$ /$1 [R=301,L]
RewriteCond $1!^(favicon\.ico¦index\.php)
RewriteRule ^(.*)$ /index.php/jamie/$1 [L]

But it works beautifully. Thank you so much. You have made my day. :)

Jamie