3

Okay, I've looked around a bit, and I cannot seem to find the answer.

Now I'm not asking for you to give me just a block of code, and say "this is the fix", I would like for you to explain how you did it, what you came up with, etc. I would really like to know more about this, and the only way to learn is by example.

Now what I need to do is:

Currently my blog (word press go figure, cause they own the world), is on the root directory. So my posts look like: http://localhost/hello-world. Just an example.

However, I've just extended my site, with a custom built PHP script using Code Igniter. I need to put the Code Igniter script on the root directory, and move the blog to /main/ or /blog/. I don't want to get rid of my blog, but I don't want Google to have to re-index every single blog post there is, or have Google lead to bad post urls. More importantly I don't want to have to sit here for hours and hours, creating redirect urls to each and every single blog post (theres hundreds of them).

Now I need the redirect to be a 301 redirect.

Heres the problem I have come across.

I NEED to have this in the .htaccess:

 RewriteEngine on
 RewriteCond $1 !^(index\.php|admin|css|img|js|main|favicon.ico)
 RewriteRule ^(.*)$ index.php/$1 [L]

This removes the ugly localhost/index.php/controller/controller-function/

But I need to make my .htaccess redirect everything on the / but NOT the preset urls I already have.

The Preset url arguements I have through Code Igniter are:

/details/
/register/
/city/
/search_process/
/login/
/logout-success/
/login-success/
/logout/
/login/
/manage/
/panel/

So in essence..

localhost/hello-world/ would have to 301 redirect to localhost/main/hello-world

and

localhost/(any-of-the-above)/ would have to NOT redirect to /main/ or /blog/

Also if you notice in the current .htaccess, I've allowed certain things like /admin/ /css/ /img/ /js/ (/main/ is the blog obviously) and favicon.ico (cause it looks awesome)

Please school me! (How often do you hear that? :P)

CREDIT GOES TO LAWRENCE CHERONE!

He originally gave me the answer, and between then and now I think I made a typo the first time, and lost the code by accident, and it didn't work the second time after copy / pasting (cause the first time i actually typed it out to learn it).

So here goes with the modified working solution. I give full credit to Lawrence Cherone (the guy who has the accepted answer for this question) because he helped me figure this out, and ultimately I couldn't have gotten this working solution without him.. Thanks again bud!

RewriteEngine on
RewriteCond $1 !(panel|manage)
RewriteRule ^(.*)$ http://localhost/choosefitness/main/$1 [R=301,L]

RewriteEngine on
RewriteCond $1 !^(index\.php|admin|css|img|js|less|favicon.ico)
RewriteRule ^(.*)$ index.php/$1 [L]

This simply states, that as long as the first argument http://localhost/choosefitness/first-argument is no /panel or /manage. It should 301 redirect to /main/ (Had to provide full URL for 301 redirect, not just main/$1)

Then it states that any request made on admin css img js lss or favicon.ico should be ignored. else should be made on index.php (this removes the index.php from the url in code igniter)

I do not know why it works i just know it does. However I havent tested it fully but I believe to be able to access /css without beign redirected, you have to add |css to the first RewriteCond. However the server is able to access the css files without needing to do so.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
David Lawrence
  • 659
  • 1
  • 8
  • 15
  • Note. ANY URL not Conditioned in the first RewriteCond does NOT work and will be sent to /main/. So http://localhost/choosefitness/css will not work because it is not In the first RewriteCond. Secondly if the URL argument is not in the second condition as well, it will not be ignored and will be acted towards the index.php. So not have |css in the second RewriteCond but having it in the first RewriteCond will act as if http://localhost/choosefitness/index.php/css, and act as a Code Igniter argument. – David Lawrence Apr 17 '12 at 01:22

1 Answers1

2

This is untested & pretty sure index.php/$1/$2 is wrong but... You could include a rule that matches your CI rules and then pass to the CI controller, and if not match is found then rewrite to your /blog/ url. Also dont forget to escape the . in the favicon\.ico part.

RewriteEngine on
RewriteCond $1 !^(index\.php|admin|css|img|js|main|favicon\.ico)

RewriteRule ^(details|register|city|search_process|login|logout-success|login-success|logout|login|manage|panel)/(.*)$ index.php/$1/$2 [L]
RewriteRule ^(.*)$ /blog/$1 [L]

Edit: If your parameter is at the front then switch the rule to something like this:

RewriteRule ^(.*)/(details|register|city|search_process|login|logout-success|login-success|logout|login|manage|panel)$ index.php/$1/$2 [L]
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • OK close. This is what I have: RewriteRule ^(details|register|city|search_process|login|logout-success|login-success|logout|login|manage|panel)(.*)$ ./index.php/$1 [L] Thats what I changed. Its now making requests on /blog/index.php/panel – David Lawrence Apr 16 '12 at 20:33
  • Not Found The requested URL /blog/index.php/panel was not found on this server. – David Lawrence Apr 16 '12 at 20:33
  • What url are you attempting to use: something like `./city/bla` would hit the CI rule while `./something/bla` would hit the blog url, it looks like you are trying `./index.php/panel` that wont match you need to use `./panel/` forget the `index.php` part. – Lawrence Cherone Apr 16 '12 at 20:37
  • When I go to http://localhost/choosefitness/panel it says: Not Found The requested URL /blog/panel was not found on this server. If i go to same url but trailing slash panel/ it says: Not Found The requested URL /blog/index.php/panel/ was not found on this server. – David Lawrence Apr 16 '12 at 20:41
  • I suppose the rule could be swapped/reversed to allow for `./index.php/panel` – Lawrence Cherone Apr 16 '12 at 20:41
  • 1
    np, future reference. a handy bookmark worthy site that explains allot of cool things with mod_rewrite: http://corz.org/serv/tricks/htaccess2.php – Lawrence Cherone Apr 16 '12 at 20:55
  • This actually, I do not know what I did, or Did not do but it stopped working. I lost the .htaccess file (stupid me quickly copied and pasted cause I wanted both my sites to have similar .htaccess files cuase i liked it better than my previous method) and I copied and pasted over the new file with the old files contents. I copied and pasted what you have here, and it doesnt work anymore. I do not know what happened what changed, best thing I can think of is that I made a typo the first time that i didnt the second. Please see my updated post above. I will give you credit, see what above. – David Lawrence Apr 17 '12 at 00:42