Log in

View Full Version : .htaccess help to rewrite URLs



Beverleyh
06-03-2009, 08:02 AM
I'm using a php script that dynamically generates news articles and creates their urls in the following format:
http://www.website.com/news.php?pmax=5&&view_news=2009-02-27-21_1235769160
As you can probably guess, the only things that changes with each page is the string of numbers on the end that represents the dates of each article.

Anyway, I've noticed that the news articles are also being accessed via a url with only one "&" in the middle, although I'm not sure where this is coming from because all the links on my website point to the "&&" url.
This is throwing up problems in my stats/page views counters as one lot of data is being generated for this url:
http://www.website.com/news.php?pmax=5&&view_news=2009-02-27-21_1235769160
and another lot of data is being generated for this url:
http://www.website.com/news.php?pmax=5&view_news=2009-02-27-21_1235769160

As they are both the same article, I was wondering if .htaccess could be used to rewrite the url and only serve the "&&" url when the "&" one is requested?
(like saying, if a url beginning with "http://www.website.com/news.php?pmax=5&view_news=" is requested, change that part to "http://www.website.com/news.php?pmax=5&&view_news=" before serving the appropriate page)

I found this thread here which looks quite promising:http://www.webmasterworld.com/apache/3292629.htm but as I'm almost totally new to .htaccess, I couldn't get my head around it, so if someone could show me how to adapt it for my needs, that would be great.

Thanks

Beverley

hyde360
06-07-2009, 02:45 AM
Hello,

Yeah those type of URLS are ugly from the start to end, and to be honest not every user will be able to remember an entire string like that.

First, it's always best to get some little basic knowledge of working with ".htaccess" files. And to be honest with you it's really easy if you take your time to understand how they work :).

Anyways, here's a little code i wrote up (which has been tested):



Options +FollowSymLinks
RewriteEngine on
RewriteRule view/(.*)/$ news.php?view_news=$1


Now to break it down...

(.*) is where the values will go, like the "view_news" would go into the first one after "view/".

Which would output it as:

http://www.yoursite.com/news/view/2009-02-27-21_1235769160/

Now you can change "view" to another value like when the article was posted or the author who posted the article.

Although make sure, you upload the ".htaccess" file into the directory folder as where "news.php" is, otherwise it would not work and you might just get yourself a nice pretty "500 Internal Server Error" displaying until it's fixed.

Hope this helps ;)

Beverleyh
06-11-2009, 10:49 AM
Hi,

Thanks for replying - I tried your code but it doesnt work for me.

I'm sorry, I really have tried to understand how to breakdown the rewrite rule but I think my problem lies in trying to pluck out the "&&" from the middle, change it and then add the date variable to the end - I just cant get my head around how to do it.

You appear to have provided a path to a "view" directory in the rule code you gave which I dont have - the news.php page is in the home directory.
There is also no reference to the "pmax=5&&" part which has confused me since that is the part I'm trying to change.
If it helps, the dynamically inserted articles are stored inside a directory called "news_articles" (/news_articles, from the root)

I tried adapting your code to this (along with other variations):
RewriteRule (.*)/$ news.php?pmax=5&&view_news=$1
which seems to follow the pattern you started but without luck - I'm just really drawing a blank on this one.

Any other suggestions?

Thanks

Beverley

Beverleyh
06-11-2009, 12:18 PM
OK - I'm doing some tests to prove to myself that I'm not going mad.

I tried this basic rule with dummy "old.html" and "new.html" files on my hosting server, and it worked fine: RewriteRule ^old\.html$ new.html
I typed the "old.html" path into my browser and it jumped to "new.html" = great!

I then tried it again using existing pages on my site and all I got was a 404 error: RewriteRule ^news\.php$ contests.php
The strange thing about it is that doing this also made the "news.php" file unreadable in a browser, even after a hard refresh and new and alternative browser instances. The news.php file looked to be sat there in all its glory on the server but I had to delete it totally, upload a backup and hard fresh the page to get it to show again.

So maybe php files are the problem - Why would a php file cause problems with a rewrite rule though?

(following on from my first post, the code I was hoping to use was this:
RewriteRule ^news\.php\?pmax\=5\&view_news\=2009-02-27-21_1235769160$ news.php?pmax=5&&view_news=2009-02-27-21_1235769160 which doesnt work. Also, I'm not sure if dashes and underscores are special characters in the "old" url position, but I tried it both ways, with and without preceding backslashes, and neither worked.)

Can anyone offer any further assistance please?

Thanks

Beverley