Home .htaccess to rewrite static page to another page
Reply: 1

.htaccess to rewrite static page to another page

Cody Codes
1#
Cody Codes Published in 2018-02-12 10:45:47Z

I want www.example.com/blog.php to redirect to www.example.com/blog.php?post=welcome without breaking the .php script.

Simply redirecting /blog.php to /blog.php?post=welcome will break the file and return a page loading error.

I want /blog.php to immediately redirect to /blog.php?post=welcome, and want all the functions of blog.php to work as is.

I tried .htaccess rewrite code but its not working.

RewriteEngine On
        RewriteCond %{HTTP_HOST} !blog.php$ [NC]
        RewriteRule ^(.*)$ blog.php?post=welcome [L,R=301]

Please help with the rewrite code.

Thanks

anubhava
2#
anubhava Reply to 2018-02-12 10:48:58Z

You can use:

RewriteCond %{QUERY_STRING} ^$ [NC]
RewriteRule ^blog\.php$ %{REQUEST_URI}?post=welcome [L,R=301,NC]

RewriteCond %{QUERY_STRING} ^$ [NC] ensures that redirect happens only when query string is empty this preventing a redirect loop.

Make sure to test in a new browser.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.341854 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO