Forum Moderators: phranque

Message Too Old, No Replies

Both redirect and remove index.php

         

porcupine

4:45 pm on Sep 5, 2010 (gmt 0)

10+ Year Member



Hi all,

Here's my issue:

I have an Expression Engine installation and I want to remove index.php from the URL by using following method. (Hosting provider says it is the only permitted way on their servers.)

#REMOVE index.php
RewriteEngine on
#RewriteCond $1 !^(images|system|etc...) [NC]
#RewriteRule ^(.*)$ /index.php/$1 [L]


The method above works pretty well alone. Which means that for the URL [mydomain.com...] I get [mydomain.com...]

Now I also want to create a redirection.
The following code in my htaccess file aims to redirect all entries to a new directory, called v1. So I write:

#Redirect
RewriteEngine on
RewriteRule ^dir_one/dir_two/(.*)$ http://mydomain.com/dir_zero/dir_one/dir_two/$


This works also perfectly well alone. Which means that I get the redirection I want to:
[mydomain.com...] redirects to [mydomain.com...]

However I can't have both codes together. Instead I get a 404 error message. What might be wrong?

Note I am complete idiot when it comes to such issues, so please explain me carefully. Any help would be much appreciated.

g1smd

8:16 pm on Sep 5, 2010 (gmt 0)

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



The way to remove "index.php" from your URLs is to remove "index.php" from the links on your pages. That is the very first step.

The rewrite code accepts those requests and connects the request to the internal script or file that will return the correct content.

Your redirect code currently generates a 302 redirect. You must add the [R=301,L] flags to that rule.

The redirect must be listed first, before the rewrite.

The rewrite code also needs a preceding RewriteCond looking at THE_REQUEST so that only direct client requests are redirected.

To miss that out results in an infinite redirect - rewrite - redirect - rewrite loop.

porcupine

9:57 pm on Sep 5, 2010 (gmt 0)

10+ Year Member



Thank you g1smd for your response.

The thing is I am quite dumb when it comes to such issues. I tried to follow your advice but I am not sure what I am doing.

Would you mind re-write the correct code in your answer? (Especially the part concerning the Rewrite Cond with the THE_REQUEST part).

Would that be too much?
Thanks a lot.

g1smd

7:25 am on Sep 6, 2010 (gmt 0)

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



Check previous threads in this forum for example code. This is a question that has been asked, in some form or another, at least once every week for the last ten years. There are many very long threads with example code and a full explanation of how it all works.

porcupine

2:19 pm on Sep 7, 2010 (gmt 0)

10+ Year Member



Found out what happened.
I just needed to add [R=301,L] to the end of the redirect part. Nothing more.

Thank you g1smd for your help.

g1smd

6:00 pm on Sep 7, 2010 (gmt 0)

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



Let's see the full code for the redirect and the rewrite.

porcupine

8:43 pm on Sep 7, 2010 (gmt 0)

10+ Year Member



OK, here it is:

#Redirect
RewriteEngine on
RewriteRule RewriteRule ^dir_one/dir_two/(.*)$ http://mydomain.com/dir_zero/dir_one/dir_two/$ [R=301,L]

#REMOVE index.php
RewriteEngine on
RewriteCond $1 !^(images|system|themes|etc...) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]


Does it make sense to you?

g1smd

9:55 pm on Sep 7, 2010 (gmt 0)

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



Duplicated RewriteRule RewriteRule, delete one.

Duplicate RewriteEngine, delete the second one.

porcupine

10:04 pm on Sep 7, 2010 (gmt 0)

10+ Year Member



Oh sure. My bad.
Thanks.