简体   繁体   中英

.htaccess rewrite rule for redirection for fixed pattern URL

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.

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, ie

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]

Following rule should work for you:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM