0

I've just spent the last 2 hours trawling through Google and the Stackoverflow archives to see if I could find an answer to my question and instead of finding nothing, I've found too much! So unfortunately I'm having to add to the mountain of 301 redirect questions. Sorry. And thanks for taking a look at this one ;)

Basically, I've got a blog for which I'm looking to simplify the URL. Currently the URLs look like this:

http://tempertemper.net/post.php?s=2012-05-30-freeagent

I'd like them to look like this:

http://tempertemper.net/2012-05-30-freeagent

I've tried adding RewriteRule /(.*)$ /post.php?s=$1 to my .htaccess file but it's not having it.

The full file currently looks like this:

<ifModule mod_rewrite.c>

DirectoryIndex index.php index.html

ErrorDocument 404 http://tempertemper.net/error.php

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.tempertemper\.net$
RewriteCond %{REQUEST_URI} !cron.php
RewriteRule ^(.*)$ http://tempertemper.net/$1 [R=301,L] 

</ifModule>

I've tried adding this after the canonicalisation bit that redirects www. to non www.:

RewriteRule ^([a-zA-Z0-9_-]+)$ post.php?s=$1

It sort of works. I can then link to http://tempertemper.net/2012-05-30-freeagent but the problem is, http://tempertemper.net/post.php?s=2012-05-30-freeagent still exists. This is more than likely bad for seo as I guess I'll have duplicate content, in the eyes of the search engines. I've tried putting a [R=301] on the end of the new line but it stops the new link working. Also tried putting the ,L in the square brackets and removing it from the canonicalisation command, but no joy (L is for last command, isn't it…?).

Basically, I'm after all URLs that generate or have generated with post.php?s= in them to permanently redirect to the ones without post.php?s= so that any links that come in to those pages already are redirected and any links in the future will direct straight to the new-look URL.

Hope that makes sense…

Thanks,

Martin :)

Martin
  • 351
  • 2
  • 8
  • 17

1 Answers1

0

Apache actually fixes local paths => proper fully qualified names (my original assumption), but I see that your file actualy checks for the wrong files. Using your supplied file I think this will work as the file contents:

DirectoryIndex index.php index.html
ErrorDocument 404 http://tempertemper.net/error.php

RewriteEngine On
# don't redirect post.php to itself
RewriteCond %{REQUEST_URI} !post.php
# don't redirect error.php to post.php 
RewriteCond %{REQUEST_URI} !error.php
RewriteRule ^(.*)$ "/post.php?$1" [R=301,L]
Sylwester
  • 47,942
  • 4
  • 47
  • 79
  • Hi Sylwester, I've tried adding that but it's taking me to this page when I load the site: http://tempertemper.net/post.php?s=post.php and giving me the error message "The page isn't redirecting properly. Firefox has detected that the server is redirecting the request for this address in a way that will never complete. This problem can sometimes be caused by disabling or refusing to accept cookies." – Martin Aug 08 '12 at 11:55
  • It was because post.php macthes the rewrite-rule and does an infinite loop. I rewrote my answer and think the supplied code will work in place of your .htaccess file. – Sylwester Aug 08 '12 at 13:07
  • Hmmm… It takes me to http://tempertemper.net/post.php when I enter any url at http://tempertemper.net, including the root. Only, strangely, it strips out the CSS. It also displays all posts on the one page, which I guess would make sense. Maybe it's how I'm writing it in alongside the canonicalisation redirect? I tried removing that but the same thing happened… – Martin Aug 08 '12 at 13:15
  • Yes. You can fix that by change the RewriteRule from ^(.*)$ to something that matches the date pattern (eg. ^/([0-9]{4}-[0-9]{2}-[0-9]{2}-.*)$ and then you can remove the RewriteCond too. But then all your URLs need to start with a date. Anternatively you just add the static urls as more RewriteConds – Sylwester Aug 08 '12 at 13:27
  • I actually plan to remove the date from the URL too, but thought that would be better tackled after this first stage. Seems not so maybe I should do it all in a oner! I can remove the date from the URL via the CMS control panel (it's in there by default to ensure each post has a unique URL). So say the URL is, using the previous example, http://tempertemper.net/post.php?s=freeagent how would I make ithttp://tempertemper.net/freeagent – Martin Aug 08 '12 at 13:41
  • How about ^/([-_A-Za-z0-9]+)$ ?. It will match alphanumeric pattern from root but not including slash or dots so static files under directories or / won't match. – Sylwester Aug 08 '12 at 15:02