Search code examples
apache.htaccessmod-rewriteurl-rewriting

Remove URL parameter and index.php filename from URL


I need some push with this, I have website that URL structure looks like this

domain.com/folder/index.php?name=asdasdAs2

How do I remove this part of the URL index.php?name= so it looks like this

domain.com/folder/asdasdAs2

The passed value (asdasdAs2) sometimes also contain these symbols ( . ) or ( - ) or ( _ ) so it will look like this (asd.asdAs2) or (asdasd_As2) or (asd-aSd-As2)

I've been trying with this code in my .htaccess file but it doesn't work.

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

RewriteRule ^/folder/(.*) /folder/index.php?name=$1

and this

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>

and this one too

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)$     $1/index.php?name=$2 [L]

The last one does the job but only with alphanumeric word ex.(asdasdAs2) but not on (asd.asd) or (asd_asdas2)


Solution

  • With your shown samples please try following .htaccess rules file.

    Please make sure to:

    • Keep your .htaccess file along with folder named folder.
    • Clear your browser cache before testing your URLs.
    RewriteEngine ON
    ##External redirect rules here.
    RewriteCond %{THE_REQUEST} \s/(folder)/index\.php\?name=(\S+)\s [NC]
    RewriteRule ^ /%1/%2? [R=301,L]
    
    ##Internal rewrite rules from here...
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]*)/([^/]*)/?$ $1/index.php?name=$2 [QSA,L]