Welcome Guest, Not a member yet? Register   Sign In
Everyone's favorite subject...URL rewriting [Solved]
#1

[eluser]kettch[/eluser]
I've got my new site/application running great with CI but I have one last issue that is driving me nuts.

The old site used the URL format of: http://mysite.com/index.php?view=testid where "testid" is just a string label for the requested content. The new site uses http://mysite.com/page/id/testid where "testid" would be the requested content.

I'm trying to write a rewrite rule that would map the old URL to the new URL and can't seem to get it right. My current rule that I tried (and doesn't work) is:

Code:
RewriteRule ^index.php?view=(.*)$ page/id/$1 [NC,R]

I've seen plenty of examples of going the other way around (from pretty URLs to query string URLs) but not from query string URLs to pretty URLs.
#2

[eluser]TheFuzzy0ne[/eluser]
You still need to route it through the index.php file, so:

Code:
RewriteRule ^index.php?view=(.*)$ index.php/page/id/$1 [NC,R]

would probably work a bit better.
#3

[eluser]kettch[/eluser]
[quote author="TheFuzzy0ne" date="1240468726"]You still need to route it through the index.php file, so:

Code:
RewriteRule ^index.php?view=(.*)$ index.php/page/id/$1 [NC,R]

would probably work a bit better.[/quote]

I suppose I should have posted the full .htaccess cause I had a feeling someone was going to mention that. :-) I have another rule that handles the rewrite the eliminate the index.php portion of the URL, which works great.

My full .htaccess is:

Code:
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteRule ^((images|styles|scripts|editor)/*.*)$ $1 [L]
RewriteRule ^index.php?view=(.*)$ page/id/$1 [NC,R]
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]

Like I said...the new site works great with the URL http://mysite.com/page/id/testid, I just want to make the previous URL format forward to the new style.
#4

[eluser]kettch[/eluser]
Dang it...it cut off the full htaccess code. *shakes fist in anger*

Here's the full htaccess at pastie: http://pastie.org/455337
#5

[eluser]TheFuzzy0ne[/eluser]
I don't fully understand htaccess files, despite my best efforts. htaccess has whooped my behind every time I've tried to get a bit adventurous, however, I keep seeing this line:

Code:
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)

And I don't understand how that's meant to work. I would recommend trying this instead:

Code:
RewriteCond %{REQUEST_URI} !^(index\.php|robots\.txt|favicon\.ico)

Do you have access to your VHost? If so, it's possible to use the RewriteLog directive to log the rewrite transactions, which can help you debug the problem.
#6

[eluser]Daniel Moore[/eluser]
I have an easier solution for you, if you don't mind a small hack to your index.php file itself. It is much less painful to implement.

Open up your main index.php file, and right after the opening <?php add the following:
Code:
if (isset($_GET['view']))
{
  $redirect = 'Location: http://mysite.com/page/id/'.$_GET['view'];
  header($redirect);
}

Since this code will be executed before CodeIgniter is loaded, then you don't have to hack anything else. I've already taken the time to test it on my local machine, and it worked flawlessly.

Good luck with that.
#7

[eluser]kettch[/eluser]
[quote author="TheFuzzy0ne" date="1240469722"]
Do you have access to your VHost? If so, it's possible to use the RewriteLog directive to log the rewrite transactions, which can help you debug the problem.[/quote]

Yeah, this is my development system...however, enabling RewriteLog doesn't do anything. No errors, no logs. It's an Apache server on Windows so who knows why it isn't working...windows installations have always been more finicky. I'll keep trying to fix it but other suggestions are more than welcome in the meantime Smile
#8

[eluser]TheFuzzy0ne[/eluser]
That's a great idea, Daniel. Thanks for sharing.

I have to admit, I'd rather implement a hack like that than mess about with htaccess files.
#9

[eluser]xwero[/eluser]
I would make a pre system hook out of it keeping the index.php as dumb as possible.
#10

[eluser]Daniel Moore[/eluser]
A pre-system hook may be a good idea, but the above is the simplest method, which gives less room for error. As you don't have to go through the index.php, load CodeIgniter, then check for this as a pre-system hook, it will also redirect you faster and have less processor overhead.

My method will cut back on CPU usage which is important on a server that receives heavy traffic.

I have done this on 2 converted sites with excellent results. (Sites converted to CI.) The reason I did this redirect is because of the links already scattered around the internet pointing to certain areas of the site and we simply did not wish all the existing links to be broken.




Theme © iAndrew 2016 - Forum software by © MyBB