Click to See Complete Forum and Search --> : [RESOLVED] rewrite writes new url in browser


Dasher
05-24-2009, 09:53 AM
I have a rather simple rewrite I am trying to implement but it does not work the way I want it too.

RewriteEngine on
RewriteRule ^([0-9]+)/([0-9]+)/$ /index.php?id=$1&arc=$2 [r=301,L]


What I want is enter: http://www.mywebsite.com/2009/3/ and get

http://www.mywebsite.com/index.php?id=2009&arc=3

I want my server to see the second (real) address, but the users browser to see (keep) the original address. The browser gets redirected to the php address instead of keeping the more friendly address. What am I doing wrong?

AaliyahRoma
05-24-2009, 10:55 AM
RewriteEngine on
RewriteRule ^/([0-9]+)/([0-9]+)/$ /index.php?id=$1&arc=$2 [r=301,L]

tried with beggining slash?

Dasher
05-24-2009, 05:03 PM
Adding / breaks the rewrite all together.

Maybe I wasn't clear what I want.

I want the address to look like this in the browser

http://www.mywebsite.com/2009/05/

but the server needs

http://www.mywebsite.com/index.php?id=2009&arc=5 to actually see the desired page.

I thought the idea of a rewrite versus a redirect was to allow the browser to see a cleaner address. I get the rewrite to the correct page. The problem is the browser sees the second URL not the first one like I want.

Dasher
05-24-2009, 09:31 PM
I found a web site ( http://www.generateit.net/mod-rewrite/ ) that creates rewrite code for you. It came up with this;


RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?id=$1&mo=$2 [L]

With this code the friendly URL has to look like this :

http://www.mywebsite.com/2009/5.html

which it rewrites to :

http://www.mywebsite.com/index.php?id=2009&mo=5

But the browser does stay with the original URL which is what I want.

It leaves the browser with the more friendly URL, but what a mess my webpage was. No formatting what so ever. How do I get my CSS pages to load correctly? I also have other pages (php includes) that appear messed up or not found.

Does the directive RewriteBase / fix that? It doesn't seem too.

ryanlund
05-25-2009, 08:40 PM
Yeah this is a problem i faced, simply use full URL's to link to stylesheets,imgs etc..........i do this by saving SITE_ROOT in a mysql database and using that on all of them. Just depends on how your site was set up.

Ryan

P.s it shouldnt mess with your PHP includes as it uses the server location rather than the web location of the file your including

Dasher
05-26-2009, 08:33 AM
I came up with a set of rewrite rules that pretty much covers my site.

RewriteEngine On
RewriteRule ^([0-9]+)/([a-zA-Z0-9-]+)\.html$ /index.php?id=$1&title=$2 [L,nc]
RewriteRule ^([0-9]+)/([0-9]+)/([A-Za-z0-9-]+)\.html$ /index.php?id=$1&mo=$2&title=$3 [L,nc]
RewriteRule ^([0-9]+)/([0-9]+)/([0-9]+)/([A-Za-z0-9-]+)\.html$ /index.php?id=$1&mo=$2&exp=$3&title=$4 [L,nc]


It turns out most of the work was required on the web pages themselves. Restructuring all the internal links, to look more SEO friendly. The need for lots of absolute URL's instead of relative ones was a bit of a pain. But I think I have most of those corrected now.

Add some PHP entry code to rewrite the main entry page, which has specific content.

More code to have dynamic page titles. That was a nice addition.