Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hello,

 

i've been making this project in my spare time, haven't looked at it for a while. But i have some spare time so i'm going to dip my toe again.

 

I have been using the following .htaccess rules

 

RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f   
RewriteRule ^about/?$ about.php 
RewriteRule ^about/([A-Za-z\+\-]+)/?$ ./about.php?get=$1  

 

Which achieves what I wanted (initially). In that I wanted to be able to make these URLs:

www.example.com/about.php
www.example.com/about.php?get=jonathon

 

 

Become these URLs respectively:

www.example.com/about
www.example.com/about/jonathon

 

Which once i'd fiddled around and achieved it, I was pretty happy with. Obviosuly this meant some changes to my relative CSS paths, which was a bit fiddly, but I couldn't see a better way around it. Though if you know any good ideas, please let me know.

 

My scenario now is much as I enjoy that these URLs look prettier, there are 2 problems with them.

 

1 - if someone types

www.example.com/about/

for example, the css relative paths fail. So the paages aren't styled.

 

2 - Also in terms of seo it looks like duplicate content

 

Sooo, my next thought was to automatically append the trailing slash, so that ^about becomes ^about/ and ^about/jonathon becomes ^about/jonathon/ auomatically. Therefore stopping a duplicate content problem and also meaning that my css will render properly.

 

So far reading around the many many articles and examples i've seen before i've come up with this:

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://localhost/www.project_1.co.uk/$1/ [L,R=301] 

 

Which works in that 'about' does indeed become 'about/' and 'about/jonathon' does becomes 'about/jonathon/'

 

Regrettably, I always get a object not found 404 error! :mellow:

 

Which is not the desired effect, I am not particulary good, (in fact i'm not even remotely good at .htaccess) so can anyone help me out with this conundrum. I have been clearing my browser cache regulary as I tinker with it to make sure that it always using the most up to date .htaccess file.

 

Thanks in advance

 

Jonathon B)

Link to comment
Share on other sites

Hey Jonathon,

 

First, I would say that any time you're messing with rewrite rules, unless you're doing straight-up redirects, I'd recommend using an absolute path to the CSS files and the media. Just more reliable. As for the issues with .htaccess, yes, it's a pain. The complexity of a Web server, a browser's behavior, and regular expressions, all in one! Could you confirm the complete .htaccess rules you've got now, in order, and I'll take a look.

Link to comment
Share on other sites

Hi Larry,

 

Thanks for getting back to me. I think i'm going to use an absolute path to reference my css/images folders as initially it is easier. Mainly because i'm going to be creating similar url structures to the ones wordpress use.

www.example.com/blog/this-is-a-blog/

The number of forward slashes may change and its just easier using absolute paths.

 

The .htaccess rule I have been using is

Options +FollowSymLinks  
RewriteEngine on

RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f   
RewriteRule ^about/?$ about.php 
RewriteRule ^about/([A-Za-z\+\-]+)/?$ ./about.php?get=$1  

RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f   
RewriteRule ^contact/?$ contact.php 
RewriteRule ^contact/([A-Za-z\+\-]+)/?$ ./contact.php?get=$1  

RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f   
RewriteRule ^blog/?$ blog.php 
RewriteRule ^blog/([A-Za-z\+\-]+)/?$ ./blog.php?get=$1  

 

And so on for my other pages. I know that this method probably isnt the most efficent when referencing each page individually, but I was just pleased that it worked initially.

 

I then commented out all my rules and basically used this set of rules on its own:

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://localhost/www.project_1.co.uk/$1/ [L,R=301] 

 

This does work in that I get a trailing slash automatically if ones isn't given and it isn't duplicated if its already specifying the trailing slash. It also handles pages '/' wise correctly if there dynamic too like about/jonathon becomes about/jonathon/ which is the desired look. (Except the page fails :( )

 

I guess that I am not passing the get information behind the scenes and hence the 404 error. I have tried to combine these rules in order to get it to work as between these 2 rules they cover my objective. I just can't get them to work together and achieve the overall goal of automatically appending a trailing slash to ^about or ^sbout/jonathon and the imformation behind the scenes all being correct. And the page displaying

Link to comment
Share on other sites

Okay, here's what seemed to work for me. You'll need to tweak it accordingly:

 

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

# Add slashes to the end of everything, if not present:
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://localhost/$1/ [R=301,L]

# Redirect about/ and about/X to about.php?get=X:
RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f   
RewriteRule ^about/$ about.php 
RewriteRule ^about/([A-Za-z\+\-]+)/?$ about.php?get=$1  
</IfModule>

 

 

Let me know if I missed something or if it's not working. Also, to apply this to multiple scripts, you could do:

 


RewriteRule ^(about)|(contact)|(blog)/$ $1.php 

RewriteRule ^(about)|(contact)|(blog)/([A-Za-z\+\-]+)/?$ $1.php?get=$2

 

That's untested but should work.

Link to comment
Share on other sites

Thank you very very much Larry :D

 

I've learnt a lot from this .htaccess job, I'm going to try and fiddle with other kind of ideas too. I think i'll probably end up getting Apress mod_rewrite book too.

 

What are your thoughts about trailing slashes seo performace, somebody in "the game" said that generally they and others (not all i don't think) thought that without slashes was better as engines expect a true url not to have any trailing slash. I'd never thought of that before, but I see the logic. But with things like WordPress they all seem to replicate adding the trailing slash.

 

Thanks again Larry :D!!

Link to comment
Share on other sites

FWIW: SEO is 99% about content and 1% everything else. Therefore, the URL setup is really not important. The question you need to ask yourself is can a text only/non javascript browser navigate to all the links in your website. If the answer is yes, then you have succeeded in SEO optimizing your URL's.

 

Cheers!

Link to comment
Share on other sites

Floydian is definitely correct. I prefer things to be consistent, so would rather there always be a terminating slash. This would also make hits reporting more reliable.

 

Yeah I agree, i really like the look of URLs with the trailing slash, which is why I embarked on this .htaccess mission. I think I struggled getting the rule right on my local host because I was working between different folders in my localhost. So i'm going to try to fix my .htaccess files so that the rules work within folders 1 level down from my localhost.

 

Appreciate your help Larry and Floydian's input too

Link to comment
Share on other sites

 Share

×
×
  • Create New...