3

I tried and failed. Original URL

https://www.sitename.com/user/login/index.php
https://www.sitename.com/user/dashboard/index.php

URL that the browser should show

https://www.sitename.com/user/login.html
https://www.sitename.com/user/dashboard.html

This is what I tried and it doesnt work

RewriteEngine on  
RewriteBase /

RewriteCond %{THE_REQUEST} (.*)\.php  
RewriteRule ^(.*)\.php http://%{HTTP_HOST}/user/$1.html [R=301,L]  

RewriteCond %{THE_REQUEST} (.*)\.html  
RewriteRule ^(.*)\.html http://%{HTTP_HOST}/user/$1.php [L]
0

1 Answer 1

3

Could you please try following. Written and tested with your shown samples. Please make sure you clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/([^/]*)/([^.]*)\.html/?$ [NC]
RewriteRule ^(.*)$ %1/%2/index.php [L]
Sign up to request clarification or add additional context in comments.

4 Comments

can you help me with one more thing ? How can I redirect .php to .html ? For example, "sitename.com/user/login/index.php" should redirect to "sitename.com/user/login.html"
@ShijThach, your welcome. So is it only specific URL should redirect or any .php? Please confirm once.
these are not the only URLs. There will be atleast 15-20 like this. So if there is a common code to redirect all , it will be great. For example every .php will be inside a folder in "user" folder. I will add it in detail in my question. Just 5 mnts
@ShijThach, I would like to request you to open a new question and NOT edit this one please because 1 question 1 post is only encouraged on SO. Also users may get confuse(future users) if we keep changing questions, so IMHO please don't edit this question and open a new one, cheers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.