user889030 user889030 - 11 months ago 37
PHP Question

htaccess redirect url to directory file

am working on making my site urls search engine friendly and for which am rewriting urls with GET parameters and so i have done rewriting but now htaccess is not pointing that url to php file which is suppose to handle the url

my old url

www.domain.com/foo/myfile.php?nid=554


new rewritten url with php

www.domain.com/foo/554-demo-page-title


my current htaccess rules which work for old urls but not for new

Options +FollowSymLinks
RewriteEngine On
#RewriteCond %{SCRIPT_FILENAME} !-d
#RewriteCond %{SCRIPT_FILENAME} !-f
RewriteBase /
RewriteRule ^([0-9]+)-(.*) ./foo/myfile.php?nid=$1 [NC]


so i want to that both old and new urls land on /foo/myfile.php becuase myfile.php can handle both urls incase of old url it rewrite and redirect as new url , i played for few hours with htaccess rules but no success

Answer Source

You can use this rule in site root .htaccess (assuming there is .htaccess inside foo/ directory):

Options +FollowSymLinks
RewriteEngine On 
RewriteBase /

RewriteRule ^foo/(\d+)-.*$ foo/myfile.php [L,QSA,NC]

If you already have /foo/.htaccess then use this rule inside that file:

Options +FollowSymLinks
RewriteEngine On 
RewriteBase /foo/

RewriteRule ^(\d+)-.*$ myfile.php [L,QSA]