We help IT Professionals succeed at work.

URL Rewrite with links

abenbow
abenbow asked
on
ok
i have a link that looks like this

domain.com/site/categories/product1.html

which I want to redirect to

domain.com/site/product1.html

this is what I am currently using to redirect and it's not working

RewriteEngine on
RewriteRule ^([A-Za-z0-9_]+).html$ /site/index.php?page=$1&done_redirect=1
RewriteRule ^([A-Za-z0-9_]+)/([A-Za-z0-9_]+).html$ /site/index.php?page=$1&id=$2&done_redirect=1

RewriteCond %{QUERY_STRING} page=([A-Za-z0-9_]+)
RewriteCond %{QUERY_STRING} !id=([A-Za-z0-9_]+)
RewriteCond %{QUERY_STRING} !done_redirect=1
RewriteRule ^site/index.php$ /1.html? [R]


what happens is

domain.com/site/home.html becomes domain.com/site/index.php?page=home

or

domain.com/site/categories/product1.html becomes domain.com/site/index.php?page=categories&id=product1

that works

the second part is that

domain.com/site/index.php?page=home shows in the address bar as  domain.com/site/home.html

but this bit does't display as I would like

domain.com/site/index.php?page=categories&id=product1 should become

domain.com/site/product1.html

Comment
Watch Question

try this

RewriteEngine On
RewriteRule ^([^/]*)\.html$ /site/index.php?page=$1 [L]

Author

Commented:
that works for

domain.com/site/link.html

but not for

domain.com/site/categories/product1.html - I get 'page can not be found' which should happen.

Author

Commented:
sorry

i mean should not happen.
add this

RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /site/index.php?page=$1&id=$2&done_redirect=$3 [L]

Author

Commented:
domain.com/site/categories/product1.html

still gives me a page can not be found.

page should be categories.html

(if I use domain.com/site/categories.html that will work)

 

Author

Commented:
ok

let me try and put it another way

this is the link that the user with click on - /categories/catergory1.html

this is what I would like to be displayed in the address bar /category1.html

and this is what I would like the 'code' to get /index.php?page=categories&id=category1

now it also needs to be able to cope with

this is the link that the user with click on - /page.html

this is what I would like to be displayed in the address bar /page.html

and this is what I would like the 'code' to get /index.php?page=page

Author

Commented:
ok, forget this I have discovered a problem with my method

Explore More ContentExplore courses, solutions, and other research materials related to this topic.