benzo
benzo

Reputation: 55

.htaccess - rewrite rules ignored or not working

This is my test .htaccess

    RewriteEngine On
    Options +FollowSymLinks    

    #WWW redirect WORKS
    RewriteCond %{HTTP_HOST} !^$
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    RewriteRule ^home/$ index.php [L] #THIS RULE  WORKS
    RewriteRule ^news/(.+)/$ /news.php?page=$1 [L] #not works
    RewriteRule ^news/(.+)/$ /news.php?article=$1 [L] #not works
    RewriteRule ^news/$ news.php [L] #WORKS

I want to make this:

"news.php" => "news/"    
"news.php?page=3" => "news/3/"
"news.php?article=lorem-ipsum-3" => "news/lorem-ipsum-3/"

With this url rules, a url like this works well, but is not what i want:

http://www.xxxxxx.com/news/?page=3  #works
http://www.xxxxxx.com/news/?article=lorem-ipsum-2 #works

how can I fix the .htaccess?

thanks

Upvotes: 1

Views: 2006

Answers (1)

miah
miah

Reputation: 10433

The only issues I see are that you have two rules that match the same conditions(L flag stops it from checking more rules) and you have an extra / in front of the rewritten path.

RewriteRule ^news/(.+)/$ news.php?page=$1 [L]
RewriteRule ^news/(.+)/$ news.php?article=$1 [L] # Will never execute

Other than that, they look fine. ( I ran them through the htaccess tester and they terminated at the correct rule.

If you aren't providing explicit canonical links, and you aren't using the .htaccess to manually add a / at the end if it is missing, you might want to consider making the final / optional with a ?

RewriteRule ^news/(.+)/?$ news.php?page=$1 [L]

Update Assuming it is a page if it is a number only, and an article if it is anything else then:

RewriteRule ^news/([0-9]+)/?$ news.php?page=$1 [L]
RewriteRule ^news/(.+)/?$ news.php?article=$1 [L]

Upvotes: 1

Related Questions