1

I have "home" directory as:

/var/www/page

I need redirect:

/var/www/page/index/product/id/9098890

Where 9098890 is the id of product.

to one file:

/var/www/page/script/index.php

I want everyone who writes in web browser:

http://example.com/index/product/id/(ANY ID)

to be redirected to script:

http://example.com/script/index.php

How to do it with .htaccess?

1

i need redirect:

To clarify, what you need is an internal URL "rewrite" (the URL in the browser's address bar does not change), not strictly a "redirect", which would imply an external "redirect" (an additional HTTP request).

Try something like the following at the top of your .htaccess file in the document root (which I assume is /var/www/page/.htaccess) using mod_rewrite:

Options -MultiViews

RewriteEngine On

RewriteRule ^index/product/id/\d+$ script/index.php [L]

Where your "ANY ID" is 1 or more digits at the end of the URL-path.

UPDATE: You can then extract the id by parsing the $_SERVER['REDIRECT_URL'] (or $_SERVER['REQUEST_URI']) superglobal in your PHP script. (But note that $_SERVER['REQUEST_URI'] also potentially contains a query string.)

Alternatively, you can explicitly pass the id to your script, as @StephenOstermiller mentioned in comments. For example:

RewriteRule ^index/product/id/(\d+)$ script/index.php?id=$1 [QSA,L]

You could then access the id value using PHP's $_GET['id'].

The QSA flag is necessary if you are expecting any additional query string parameters on the original request URL, in order to append them to the rewritten URL.

| improve this answer | |
  • I'd imagine that index.php needs access to the ID to be able to work. RewriteRule ^index/product/id/(\d+)$ script/index.php?id=$1 [L] – Stephen Ostermiller Aug 19 at 13:28
  • Awesome - very good work. I have one more trouble... because if i do rewrite it by .htaccess than requested url is show like its destiantion http:// ... script/index.php. Worst is i need id of that product to use in script (where he is rewrited). I was expect after redirect i will get requested url by something like this: $actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; but it not show where from someone come so /product/id/"number" but this directive says /script/index.php – XmanY Aug 19 at 13:33
  • For now i have in .htaccess: Options -MultiViews RewriteEngine On RewriteRule ^index/product/id/(\d+)$ script/index.php?id=$1 [L] and i dont know how to get to /srcipt/index.php this ID from oryginal link – XmanY Aug 19 at 13:51
  • And one more information: in webbrowser i have in address: domain/index/product/id/(number) but rewrite works cause its show what is in script: /script/index.php – XmanY Aug 19 at 13:59
  • Ok i track it myself: code: $id = $_GET['id'] . Works thank you very mutch – XmanY Aug 19 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.