Renaming file in URL (.php file)

Viewed 57

I have a file called filename-step1.php and looking to make the url domain.com/filename/step1 through .htaccess.

My current .htaccess file looks like:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

At the moment, this works fine for removing the .php tag from the url. Just need to fix the directories.

2 Answers

You could add a RewriteRule to redirect to $1/$2.php

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)/(.*)$ /$1-$2.php [NC,L]
</IfModule>

So, filename/step1 will trigger the filename-step1.php file.

I am giving 2 solutions here, please try one at a time only in your .htaccess file. With your shown samples, could you please try following as a Generic rules.

RewriteEngine ON
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ $1-$2.php [NC,L]


OR in case you are looking for specifically url filename/step1 try following.

RewriteEngine ON
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteRule ^(filename)/(step1)/?$ $1-$2.php [NC,L]
Related