Aman Sharma
Aman Sharma

Reputation: 173

Redirect Rule in .htaccess php

I want to redirect this URL http://www.example.com/blog/Title-here to my blog.php page

Please note Title-here can be anything.

How I can do that ?

I am trying following but it's not working.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/(.*)/?$ blog.php [NC,L]

I don't know why.

Following is my full .htaccess code, may be here's the issue.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)/?$ product.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/?$ results.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ results.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/(.*)/?$ blog.php [NC,L]

ErrorDocument 404 http://www.example.com/404.php

Upvotes: 3

Views: 104

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133528

With your shown samples, could you please try following. Since you are using very generic regex(not giving any uri condition in it) so its not reaching to your last rule of blog page. Keep it as your first rule like as follows. I have also fixed your regex in your all existing rules.

Please make sure to clear your browser cache before testing your URLs.

###Making Rewriteengine ON here.
RewriteEngine ON

##Placing rule for URIs starting from blog here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog  blog.php [NC,L]

##Placing rule for url like: http://localhost:80/test1/test2/test3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(?:[^/]*)/(?:.*)/?$ product.php [L]

##Placing rule for url like: http://localhost:80/test1/test2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(?:.*)/?$ results.php [L]

##Placing rule for url like: http://localhost:80/test1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ results.php [L]

ErrorDocument 404 http://www.example.com/404.php

Upvotes: 4

Related Questions