nitotm
nitotm

Reputation: 579

Htaccess: Hide php file behind nice seo friendly url

So I have "nice" urls:

RewriteRule ^(.*)/$ page.php?data=$1 [L]

The problem is that page.php is accessible so site.com/page.php?data=xxx will show duplicate content of site.com/xxx/

I want to prevent that, but if I do any Redirect or RewriteRule even before the nice url RewriteRule it affects/makes the file inaccessible even behind the nice url.

Is there any way?

I can change page.php to a rare filename, and in case its discovered and published, add html canonical urls, but I would like to do it right.

Upvotes: 0

Views: 227

Answers (1)

Croises
Croises

Reputation: 18671

Add this before your first RewriteCond:

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+page\.php\?data=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]

Upvotes: 1

Related Questions