Search code examples
php.htaccesshttp-status-code-301

Very advanced .htaccess


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.


Solution

  • 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]