Daniel Palade Daniel Palade - 11 days ago 7
PHP Question

How can I redirect users if using the non-rewritten url from Htaccess?

I want to make it so when you access the non-written url, for example this one: website.com/profile.php?name=Alex you would get redirected to a 404 not found page. But if you would access website.com/profile/Alex it would just work normally. How could I do this?

Answer

You can use these rules:

Options -MultiViews
RewriteEngine On

# 404 if using internal URL directly
RewriteCond %{THE_REQUEST} /profile\.php\?name= [NC]
RewriteRule ^ - [R=404,L]

# rewrite for /profile/alex
RewriteRule ^(profile)/([\w-]+)/?$ $1.php?name=$2 [L,QSA,NC]