2

I have a website with about a 100 pages for which the sample .htaccess file has

RewriteRule    ^1/?$    page1.php    [NC,L] 
RewriteRule    ^2/?$    page2.php    [NC,L] 
RewriteRule    ^3/?$    page3.php    [NC,L]  

The problem is these pages get a lot of redirection between one another and while adding

header("location: page1.php");

works, I wish to populate this using MySQL.

I can instead hard code is to

header("location: /1");

but if one of the entries in the .htaccess file changes, I have to change in many places.

So my question is can this be controlled by MySQL, like if I have a simple table of

(id,page) values (1,page1.php)    

and .htaccess gets populated based on this MySQL table.

What would be the best way to achieve this, if its possible.

Thanks.

2
  • Do the numbers always coincide in the rules like "(number) to page(number).php" or are your rules more like "(any random thing) to page(other random thing).php" ? Commented Mar 20, 2019 at 23:16
  • @joni Its actually random. Commented Mar 21, 2019 at 7:56

1 Answer 1

2

You should use RewriteMap.

It uses text file that is mentioned in .htaccess and can be used for mapping 'something' to 'something' in RewriteRule.

Something like this:

RewriteMap p2f txt:/path/to/map.txt
RewriteRule ^([^/]+)/?$ /{p2f:$1|notfound.php} [L]

map.txt would be:

1 page1.php
2 page2.php
...

Apache monitoring the file changes and re-loaded it automatically.

You will need to change your process so when something updated in database, the text file is updated (SELECT INTO OUTFILE can be used for this).

It is possible to query database directly, however when I did testing long time ago it was faster with the file.

Sign up to request clarification or add additional context in comments.

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.