Search code examples
php.htaccesshttp-redirectmod-rewritefriendly-url

Redirect with htaccess from index.php to another.php file


I want to create pretty url. But, I got some problem with .htaccess. For example I have url domain/some.php?f=query-string. I want to change domain/query-string (expected url). Is that possible to change / redirect via .htaccess. Or maybe from php file itsself.

this is a bit of htaccess snippet i made, but i get it blank/error page

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
    RewriteRule ^/([^/.]+)$ some.php?f=$1    [NC,L]
</IfModule>

Thanks for your attention.


Solution

  • RewriteRule ^/([^/.]+)$ some.php?f=$1    [NC,L]
    

    In .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash, so the above will never match and it will do nothing. This should be written like the following instead:

    RewriteRule ^([^/.]+)$ some.php?f=$1 [L]
    

    The NC flag is not required here, since the regex is already "case-insensitive".