skip to Main Content

I am trying to write .htacces rewrite rules for blog module.

if => www.xyz.com/index.php It should not redirect anything

if => www.xyz.com/contactus.php It should not redirect anything

but
if => www.xyz.com/blogs/test-string1-and-string2
(URL pattren fixed for blog pages)

It should redirect to blog_details.php?id=$1

I have tried following rules.:

RewriteCond %{HTTP_HOST} !/blog/

RewriteRule blogs/(w+)  blog_details.php?id=$1 [NC]

This is not perfect rule. It is causing problems while generating sitemaps and Seo results.

I want some strong validation for URL like .php at the end of URL or

words separated by hyphen (-) after blog/. Then I can redirect url.

2

Answers


  1. First you need not check the HTTP_HOST here, as you are not changing it.

    If you do, you should compare it to a possible Hostname, not to a possible URL, i.e.

    RewriteCond %{HTTP_HOST} !^www.nottherightone.tld$
    

    To check, if the URL does not end in “.php” and contains hyphens, this should be a possbility

    RewriteCond %{REQUEST_URI}   !.php$
    RewriteRule blogs/(.*-.*)    blog_details.php?id=$1 [NC]
    
    Login or Signup to reply.
  2. Following rule should work for you:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^blogs/(.+)$ blog_details.php?id=$1 [NC,L,QSA]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search