php - Serve HTML file if it exists, otherwise redirect to index with mod_rewrite -
i'd write rewrite rule following: if of following files exist on server static html file in specific directory i'd .htaccess
serve static file. otherwise, i'd id (first number after slash) passed query parameter www.gallery.com/index.php
www.gallery.com/1-driving-to-the-beach.html www.gallery.com/2-swimming.html
example: assume www.gallery.com/2-swimming.html
doesn't exist html file on server. want id=2 sent /index.php
can use
<?php ob_start(); ?> <html></html> <?php file_put_contents('2-swimming.html', ob_get_contents()); ?>
questions:
- what rewrite rule
.htaccess
file should contain? - how grab id in
index.php
when url redirected .htaccess?
add rule htaccess file in document root:
rewriteengine on rewritecond %{request_filenane} !-f rewritecond %{request_filenane} !-d rewriterule ^([0-9]+)-(.*)\.html$ /index.php?id=$1 [l]
that sends id portion of request index.php
parameter id.
Comments
Post a Comment