Belatar Yassine
Belatar Yassine

Reputation: 163

redirect non www to www and rewriterule

In fact, I am working on a small PHP script. I created some rules to redirect nonwww to www version here is my code:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule app-([0-9]+)\.html app.html?id=$1 [L,QSA] 
AddType application/x-httpd-php5 .html .htm

It works fine when redirecting the home page but when i want to redirect site.com/app-9.html for example it just redirect to the www version like this : www.site.com/app.html?id=9. It should redirect site.com/app-9.html to www.site.com/app-9.html. How can I achieve this please ?

pass:

site.com/app-9.html

current result:

www.site.com/app.html?id=9

required result:

www.site.com/app-9.html

Upvotes: 1

Views: 57

Answers (2)

Rohit Goyani
Rohit Goyani

Reputation: 1256

Added validation for app-9.html only, Becuase the rule is required for others.

Use this:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

## added validation for app-9.html only
RewriteCond %{REQUEST_FILENAME}  !app-9.html -f
RewriteRule app-([0-9]+)\.html app.html?id=$1 [L,QSA] 

AddType application/x-httpd-php5 .html .htm

Upvotes: 1

user3163495
user3163495

Reputation: 3518

Can you insert

RewriteRule ^/page /page?

into your rewrite rule? This will remove the query string altogether.

Upvotes: 0

Related Questions