PDA

View Full Version : Understanding Apache and modrewrite.


Dave A
10-08-2007, 06:00 PM
Are there any apache modrewrite experts around? I'm struggling to get some rewrites working as they should.

Paz
10-08-2007, 08:32 PM
Well I have a cousin who knows a chap who did a mod-rewrite once; maybe I can have a look... LOL

Seriously, post an example.com url you want to re-write - the more detail you can give the better - and we can have a look for you.

Cheers,
Paz.

Dave A
10-09-2007, 03:07 PM
hokay - here we go.

I've been working on two SEO issues around website URLs.

First, there's the www or no www issues which I've got well sorted.

However, the index.php or no index.php has proved somewhat trickier. I've used actual examples to show that the concept works, but my rewrite doesn't for some reason.

My goal is to take a URL such as
http://www.theforumsa.co.za/index.php?page=competence
and turn it into
http://www.theforumsa.co.za/?page=competence

Now I would expect this little piece of code in the .htaccess to work, but it doesn't.
RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,NS,L]
Any ideas?

Snoopy_inc
10-11-2007, 07:27 AM
Your best idea would be to speak to VB Coders... since they put the code to output that url.

The should be able to point you in the right direction pretty fast.

Dave A
10-11-2007, 07:47 PM
I've found a few suggested php based solutions for a 301 redirects for this, but they all seem to come with problems. However, it shares the same principle of treating external and internal requests differently.

This would be way more elegant with a modrewrite solution, not to mention less vulnerable to being affected by upgrades. And there simply has to be a way.

I just wish the Apache manual was more... instructive. It seems to rely heavily on a prior knowledge of PERL, which I don't have. And there is a subtlety here that I'm simply missing.

Paz
10-14-2007, 07:37 PM
My goal is to take a URL such as
http://www.theforumsa.co.za/index.php?page=competence
and turn it into
http://www.theforumsa.co.za/?page=competence


I wouldn't do this. If you're going to the trouble of doing a re-write, I'd rewrite without the query string eg:
http://www.theforumsa.co.za/competence

Let me know if you're interested and I'll give you the code.

Dave A
10-15-2007, 05:52 AM
I'm interested!!!

Thanks, Paz.

Snoopy_inc
11-21-2007, 09:05 AM
I have one for you.

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]

Doesnt help if you have subdomains.

What can i do for this if i want the change over just for my main domain only?

Dave A
11-21-2007, 01:00 PM
Hmm. Depends on the natue of the error with the subdomains.

Got URLs for me to look at?

Snoopy_inc
11-22-2007, 09:12 AM
had a blog site called http://iblog.wellinformed.co.za Which is broke now
And have another sub that i created called es.wellinformed.co.za which is also broke :P

It has to do with replacing all http:// to www equivilent

Paz
11-22-2007, 08:33 PM
Hi Snoopy,

It's fairly easy to redirect your non www pages without affecting your sub-domains. I had the same problem redirecting non-www pages here without losing forums.digital-m.co.za

Give me a list of all your sub-domains and I'll post the code for you here.

Cheers,
Paz.

Snoopy_inc
11-22-2007, 08:47 PM
Hi Paz,

Thanks for the help:

here are but a few of the sub domains i use.... ill modify your code with the extra ones when you paste it.... will be simple to modify when i see the code myself ;)

es.wellinformed.co.za
iblog.wellinformed.co.za
itrade.wellinformed.co.za

Thanks

Paz
11-23-2007, 11:51 AM
Hi,

I've just looked at our htaccess and we don't need the domain list at all - I'd forgotten!

This one just redirects http://example.com pages;

RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} ^digital-m\.co\.za [NC]
RewriteRule (.*) http://www.digital-m.co.za/$1 [R=301,L]

Let us know how it goes. And if it does cause the server to fall over, be sure to send us the relevant part of your error.log file.

Cheers,
Paz.

Snoopy_inc
11-23-2007, 03:07 PM
Will that allow my subdomains to work paz?

it seems that the condition is ^wellinformed\.co\.za will convert everything before the ^
Isnt that the problem?

Plz help me understand this.... im curious now ;)

Paz
11-23-2007, 06:35 PM
Hi,

Yeah that'll work with your sub-domains. As I say that's the htaccess we use with out digital marketing website.

The ^wellinformed\.co\.za line only redirects urls that don't have anything in front of the "wellinformed" part.

Give it a try!

Cheers,
Paz.

Snoopy_inc
11-24-2007, 03:58 PM
seems to have worked nicely .... could you check if im getting sessions now again? or duplicate home pages?

Thanks paz for your help.

Paz
11-25-2007, 06:30 PM
Hi,

Glad the redirects are working OK.

I'm seeing fewer duplicates and it'll get better but you still have session ID's eg;
/forum/index.php?PHPSESSID=f695beaefc4b15be6c154906cb5d88 36&action=profile;u=1

They'll prevent your forum pages from being indexed, which is a pity because you're forum's taking off and you could really boost your indexed pages. You need to switch them off in the admin panel by disabling cookies. Give me a temporary admin login by PM if you can't find it.

Cheers,
Paz.

Snoopy_inc
11-26-2007, 05:40 AM
I am assuming you are talking within the forum software Paz. If so the only options i have are these :

1)Enable local storage of cookies
(SSI won't work well with this on.)

2)Use subdomain independent cookies
(turn off local cookies first!)

3)Default login cookies length (in minutes)

Is that what you talking about?

Snoopy_inc
11-26-2007, 05:51 AM
check this out paz.... http://www.simplemachinesforum.net/community/index.php?topic=74.0

seems like they talking about the sessions also. And figured out a way... confirm im not just seeing what i want to see ;)

Paz
11-26-2007, 07:27 AM
I have to register.. can you cut and paste the relevant part of the text...

Snoopy_inc
11-26-2007, 05:05 PM
Bah they fixed the main site and i cant find the url anymore:

http://codingforums.com/showthread.php?t=80113

They seem to have this issue on SMF forums..... strange really.

Dave A
12-19-2007, 12:30 PM
Try

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www
RewriteRule ^sitename$ http://www.sitename/$1 [R=301,L]

Blazingpie
04-07-2008, 11:23 AM
Still getting problems?