0

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
  • But there is no ".php" at the end of the URL you want to redirect (www.xyz.com/blogs/test-string1-and-string2)? Or is this meant to be an inclusive "or"? Commented Mar 15, 2015 at 10:54
  • yes, if there is no .php in URL then I want to redirect. Commented Mar 15, 2015 at 10:56

2 Answers 2

0

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]
Sign up to request clarification or add additional context in comments.

2 Comments

Its working .. but I am creating sitemaps using xml-sitemaps.com.. Its giving some fake links like www.xyz.com/blogs/index.php , www.xyz.com/blogs/contactus.php.. etc.. how to stop it ?
.php is excluded here, so it should not be caused by the rewrite rules here. Or is this another problem?
0

Following rule should work for you:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blogs/(.+)$ blog_details.php?id=$1 [NC,L,QSA]

2 Comments

Its working .. but I am creating sitemaps using xml-sitemaps.com.. Its giving some fake links like www.xyz.com/blogs/index.php , www.xyz.com/blogs/contactus.php.. etc.. how to stop it ?
This rule isn't impacting any other link except /blogs/something. /blogs/contactus.php will not be impacted due to RewriteCond %{REQUEST_FILENAME} !-f which ignores all real files.

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.