Forum Moderators: phranque

Message Too Old, No Replies

.htaccess and rewrite

domain.com/products.php?id=001 - domain.com/products/001

         

phill2000star

5:41 pm on Jun 22, 2007 (gmt 0)

10+ Year Member



Hiya everyone.

I am redesigning a site at the moment and I am looking a lot at SEO. At the moment all the products are viewed by the following fashion.

[domain.com...] etc

I am wanting to use .htaccess to produce the following...

[domain.com...]
[domain.com...] etc...

I am getting nowhere with this. Can anyone shed some light on it?

Thanks in advance!

jdMorgan

5:59 pm on Jun 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This previously-posted tutorial [webmasterworld.com], and some of the threads in our Apache forum library may be of assistance to you.

Jim

phill2000star

6:10 pm on Jun 22, 2007 (gmt 0)

10+ Year Member



Yeah I have read the pages, but still cant get it working?

So far I have the following

Options +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^books/(.*) products.php?=id=$1 [L]

Can you see anything wrong with this?

Many thanks!

sc112

6:34 pm on Jun 22, 2007 (gmt 0)

10+ Year Member



You seemed to have it backward. And there are syntax errors in your rewrite rule.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /products\.php\?id=(.+)\ HTTP/ [NC]
RewriteRule ^products\.php$ http://www.example.com/products/%1 [R=301,L]

jdMorgan

6:55 pm on Jun 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For the purposes stated, both steps are needed, and one more -- The first. Please review the cited thread in-depth, as it was very carefully written.

1) Change the links on your pages to 'friendly links'.
2) Internally rewrite those friendly URLs, when requested by a client, into the form needed for your script.
3) Externally redirect dynamic ('unfriendly') URLs to the new friendly ones if directly-requested by a client (i.e. browsers and search engine spiders).

"It doesn't work" gives us no hint as to what the problem might be. How did you test? What were the results? How did those results differ from expectations?

Jim

phill2000star

6:57 pm on Jun 22, 2007 (gmt 0)

10+ Year Member



Thanks for your input. I tried what you suggested and it didnt work. I have been reading some more and have come up with this but i still cant get it to work?

RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule ^(.*)products/(.*)$ $1products/products.php?id=$2

I have this .htaccess file in the following directory...

[domain.com...]

I am calling it by the following URL...

[domain.com...]

(which i want to point to [domain.com...]

Is it possible or am I just being thick!?

sc112

7:34 pm on Jun 22, 2007 (gmt 0)

10+ Year Member



I have just been through this same painful learning process, so I know the frustrations you are feeling.

Assuming you have rewritten all your page links into the friendly forms, you do an internal rewrite and then an external redirect.

# Internally rewrite static URLs to dynamic
RewriteRule ^products/(.+)$ /products.php?id=$1 [L]

# Externally redirect dynamic to static
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /products\.php\?id=(.+)\ HTTP/ [NC]
RewriteRule ^products\.php$ http://www.example.com/products/%1 [R=301,L]

Not knowing you don't use the querry string on the first half of the last rewrite rule above was my stumbling block. We newbies have no way of knowing that. It just seems logical to put the querry string there when you do on the rewrite condition. Hope Jim will add a note in his tutorial about that querry string. I am sure it will help many others.

phill2000star

7:43 pm on Jun 22, 2007 (gmt 0)

10+ Year Member



thanks sc112. I have tried that but no luck. I cant see what the issue may be?

Every time i try i get a 404 error. Is there anyway of logging what the .htaccess file is trying to do?

sc112

8:10 pm on Jun 22, 2007 (gmt 0)

10+ Year Member



You should put the .htaccess in the root directory.

If you use Firefox, download the extension

Live HTTP headers

You will get an idea what is happening with your redirects.

phill2000star

8:10 pm on Jun 22, 2007 (gmt 0)

10+ Year Member



Right.....think im getting there. I have come up with the following code....

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^([^.]+)$ [mydomain.com...] [L]

and it does the followin....

if i goto the following url....
[mydomain.com...]

directs the user to the following....
[mydomain.com...]

The only problem I have is that in the address bar the full url appears. I was under the impression that I could get it to display the URL in the address bar as [mydomain.com...] and not [mydomain.com...]

Does anyone know if this is possible?

sc112

8:18 pm on Jun 22, 2007 (gmt 0)

10+ Year Member



Leave out the [.......] part.

phill2000star

8:52 pm on Jun 22, 2007 (gmt 0)

10+ Year Member



hahaha.... it gives me a 403 forbidden error. The file is there and if i include the full path - RewriteRule ^([^.]+)$ [domain.com...] [L] - in the rewrite rule it works but shows the directed url in the address bar.

If i use - RewriteRule ^([^.]+)$ products.php?id=$1 [L] - then I get a 403 error?

any ideas why?

phill2000star

11:07 pm on Jun 22, 2007 (gmt 0)

10+ Year Member



Sorted it.... i was hosting it on a test server online which was addressed via IP rather than a domain name. Put it on the live server and worked perfectly.

Thank you for your assistance sc112. Its nice to see someone sharing their knowledge and experience rather than thinking people DONT read other posts and try to help themselves (as well as appearing condescending).

Although I tried to educate myself and did a bit of homework rather than post on a forum and expect people to do the thinking for me, it seems that some other people thought otherwise and posted a link to a post they had put online.

I understand frustrations of some people and answering the same question numerous times can be infuriating (try my job.... my colleagues can try a saints patience), but sometimes people ask for help because they genuinely cant come to the answer they want without a little guidance. In my case, I had a little guidance and I sorted my problem.

If only more people showed your selflessness and helpfulness on forums.