0

How to redirect in url by using htaccess in php?

http://www.example.com/randomstring/2

i would like to go to id page 20 thank

1
  • You can’t use .htaccess in PHP. What do you mean by that? Commented Dec 8, 2010 at 14:52

3 Answers 3

2

You should use mod_rewrite for example in this way:

RewriteEngine On

RewriteRule ^(randomstring)/([0-9]+)$  /randomstring/$1/ [QSA,R] 
RewriteRule ^(randomstring)/([0-9]+)/$ index.php?page=$1&id=$2 [QSA,NC,L]

If you want to simply redirect from a domain to another just write:

RewriteRule ^(randomstring)/([0-9]+)$ http://www.example.com/$1/$2/ [QSA,NC,L,R=301]
Sign up to request clarification or add additional context in comments.

Comments

1

use mod_rewrite, it's a module for apache. You can see the documentation here. .htaccess files control the webserver, not php, so you have to look there.

Comments

1

Copy that into your .htaccess file in the directory you need it:

RewriteEngine On
RewriteRule ([A-Za-z0-9]+)/([0-9]+)$ index.php?id=$2

www.domain.tld/asdf/2 --> index.php?id=2
www.domain.tld/asdfa923als/52 --> index.php?id=52

;-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.