Warm tip: This article is reproduced from serverfault.com, please click

How can I use .htaccess to redirect a dynamic URL to another Dynamic URL?

发布于 2020-11-28 06:08:34

I've looked around and my web knowledge is basic. I'm trying to make a dynamic URL link to another-- that is, where one part of the URL can be different.

In this case, I'm trying to make (With my URL scrubbed to some generic one as not to break any rules, here):

https://theurl.com/news/fullnews.php?fn_id=33

TO

https://www.theurl.com/index.php?fn_mode=fullnews&fn_id=33

Where the 33 is a number that can be different based on the article linked.

Thanks any help that can be provided! I've tried editing some similar scripts but seem to not know quite what I'm doing.

Questioner
Rocky Raccoon
Viewed
0
arkascha 2020-11-29 06:47:26

This probably is what you are looking for:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^fn_id=(\d+)$
RewriteRule ^/?news/fullnews\.php$ /index.php?fn_mode=fullnews&fn_id=%1 [END]

You need to enable the interpretation of distributed configuration files (".htaccess") for this and obviously the rewriting module needs to be loaded and enabled in the http server.

Much more elegant and preferred for SEO reasons however would be to use a URL like that:

https://example.com/fullnews/33 or https://example.com/news/33

Which could be achieved by those rewriting rules:

RewriteEngine on
RewriteRule ^/?fullnews/(\d+)$ /index.php?fn_mode=fullnews&fn_id=$1 [END]