Home     Articles & Projects     Products & Web Services     Forum

htaccess need some help

Hi Guys, need a little help.
I would like to hide URL's

I want a user to be able to go to the following
http://www.example.com/news/this-is-the-news

and for it to go to
http://www.example.com/news.php?id=1

each story in the database is stored title has a URL link like this-is-the-news but the title shows This is the news. Kinda like wordpress htaccess sorry that is the best explaining I can do.

If anyone can help that would be great.

This is what i am thinking I have to do.
When the link goes to news.php instead of the id= it has to be url=this-is-the-news and then in the first PHP code I have to do a database check to see if that link is in the database.

from there I have to show the body and the title in the right area?

Would that be correct? So in basic I need to know how to hide the URL to be more SEO friendly.

Hi Russell, What you need to

Hi Russell,

What you need to do is pass the title through to news.php; which then selects the article based on the title rather than an ID.

In your .htaccess, you would have something like this:

RewriteRule ^news/(.*)$ news.php?title=$1&%{QUERY_STRING} [L]

And then, at the top of news.php, SELECT your article based on the title; from $_GET["title"]. You might need to strip spaces depending on how you are storing / generating your SEO friendly titles; and also make sure that they don't contain any other non-URL safe characters...

Hope this helps!
Cheers,
David.

Thanks David, just wondering

Thanks David,
just wondering to go a step further can I do something like this?

http://www.example.com/index.php?t=news&title=this-is-a-story

i am trying to work out the best way to do the URL and templates

Sure, In that case the

Sure,

In that case the RewriteRule would be...

RewriteRule ^news/(.*)$ index.php?t=news&title=$1&%{QUERY_STRING} [L]

...and then of course you can have other rules that rewrite to different values of "t".

Cheers,
David.

I tryed the following but it

I tryed the following but it did not work

RewriteRule ^(.*)$ index.php?t=$1&%{QUERY_STRING} [L]
RewriteRule ^(.*)/(.*)$ index.php?t=$1&title=$2&%{QUERY_STRING} [L]

I then tryed this

RewriteEngine on
RewriteRule ^(.*)$ index.php?t=$1 [L]
RewriteRule ^(.*)/(.*)$ index.php?t=$1&title=$2 [L]

But it screws up my index.php file the CSS and JS script is not working. They seem to go to http://www.example.com/about/template/css/style.css

instead of http://www.example.com/template/.....

Thanks
Russell

Hi Russell, When you're

Hi Russell,

When you're creating a virtual directory hierarchy with .htaccess, any resources that your scripts refer to must use a fully qualified path - i.e. one that begins with "/" so that the browser knows to request the resource relative to the top level of your site - not the virtual directory that doesn't really exist.

For example; instead of:

<img src='images/logo.gif' />

...just change it to:

<img src='/images/logo.gif' />

That will be all it is!

Cheers,
David.

This was the finial code I

This was the finial code I went with, just wish I did not have to do the news/ and iphone/ etc...

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)$ index.php?t=$1 [L]
RewriteRule ^iphone$ index.php?t=iphone [L]
RewriteRule ^shows/([^/.]+)$ index.php?t=shows&a=$1 [L]
RewriteRule ^news/([^/.]+)$ index.php?t=news&artical=$1 [L]
RewriteRule ^request/([^/.]+)$ index.php?t=request&a=$1 [L]