kqlambert
kqlambert

Reputation: 2711

.htaccess file mod_rewrite clean url not working

So I want the usual thing from the .htaccess file. www.example.com/article.php?title=my_title should be replaced with www.example.com/article/my_title, however I cannot seem to get it to work. As of right now I can get the .php to drop and I suspect that might be some of the problem. (also I get http://example.com to go to http://www.example.com) Here is my .htaccess file

Options +FollowSymLinks -MultiViews 
rewriteengine on

## this is the piece that is not working
RewriteRule ^title/(.*) article.php?arr=title/$1

rewritecond %{HTTP_HOST} ^example.com$
rewriterule ^example\/(.*)$ "http\:\/\/www\.example\.com\/$1" [R=301]

RewriteBase /

## Hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

## Error document (needs to have a 404 page created)
ErrorDocument 404 http://www.example.com/

Upvotes: 1

Views: 388

Answers (1)

Jon Lin
Jon Lin

Reputation: 143856

You need to add these specific routing rules before any rules that generally apply to php files. So just under your rewriteengine on, add:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/article\.php\?title=([^&\ ]+)
RewriteRule ^ /article/%1? [L,R=301]

RewriteRule ^/?article/(.*)$ /article.php?title=$1 [L]

Then the rest of your rules.

Upvotes: 1

Related Questions