3

The following code works to hide .php and replace it with .html

RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/([^/]*)/([^.]*)\.html/?$ [NC]
RewriteRule ^(.*)$ %1/%2/index.php [L]

I would like to redirect all .php requests to .html. All .php files are inside a sub directory in "https://www.sitename.com/user/". For example

https://www.sitename.com/user/login/index.php
https://www.sitename.com/user/name/index.php
https://www.sitename.com/user/register/index.php
https://www.sitename.com/user/logout/index.php
https://www.sitename.com/user/dashboard/index.php
https://www.sitename.com/user/contact/index.php

It should redirect to

https://www.sitename.com/user/login.html
https://www.sitename.com/user/name.html
https://www.sitename.com/user/register.html
https://www.sitename.com/user/logout.html
https://www.sitename.com/user/dashboard.html
https://www.sitename.com/user/contact.html

Adding separate line of .htaccess code for each folder will be difficult, can someone help with simple code to automatically detect and redirect .php to .html ?

Explanation: If some try to access "sitename.com/user/login/index.php", it should load "sitename.com/user/login.html".

sitename.com/user/login.html should be the only URL that is visible to users. Even if someone try to access "sitename.com/user/login/index.php", it should redirect/rewrite to "sitename.com/user/login.html". Enter sitename.com/user/login.html in browser = sitename.com/user/login.html Enter sitename.com/user/login/index.php in browser = sitename.com/user/login.html

11
  • You are confusing "rewrite" and "redirect" :-) If you want to hide something, you want to rewrite. The ? at the end of your regex should be optional I gues? Commented Dec 21, 2020 at 8:51
  • @Daniel W, what I want is to redirect requests going to original script location to fake URL (here php to html). Commented Dec 21, 2020 at 9:08
  • What exactly do you mean by "redirect"? What have you tried to make that work? Commented Dec 21, 2020 at 9:11
  • 1
    Does the same file exist with .html extension? Commented Dec 21, 2020 at 9:28
  • 3
    Please add all clarification to your question by editing it - also, explain what exactly should happen if anybody requests that "hidden" URL - should this request be forwarded to the public URL, should it show an error message? Commented Dec 21, 2020 at 10:02

2 Answers 2

3

With your shown samples, could you please try following. Please make sure you clear your browser cache before validating your URLs. As per OP's comment edited rules, as per .php and .html formats. I believe you should avoid giving both kind of urls to users and ask them to hit only .html urls in case anyone hits .php url you could forbid it(in case they are directly hitting it, another reason could be because if they are hitting .php directly then you want it to change URL on browser to .html which is actually being served by a .php file itself in backend), if they hit .html then that could be served by respective index.php of passed uri IMHO. NOTE: This is IMHO only and should not be used if someone has more requirements on this one.

RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/user/([\w-]+)\.html/?$ [NC]
RewriteRule ^(.*)$ user/%1/index.php [L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} index\.php/?$ [NC]
RewriteRule ^ - [F]
Sign up to request clarification or add additional context in comments.

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.
2

You can use the following rules to convert your php URLs into html :

RewriteEngine on

#Redirect and rewrite php URLs to html
#redirect /user/foobar/index.php to /user/foobar.html
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^user/([^/]+)/index\.php$ /user/$1.html [L,R]
#rewrite /user/foobar.html to /user/foobar/index.php
RewriteRule ^user/([^.]+)\.html$ /user/$1/index.php [L]

The rule #1) triggers when /user/foobar/index.php is requested and redirects it to /user/foobar.html . Since the .html file doesn't exist the second rule maps the .html request back to original .php page but your URL remains the same in browser address bar.

1 Comment

@SamuelLiew May I please know why all the comments posted on this answer were removed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.