How to hide extension like '.php'

 
Post new topic   Reply to topic    Aprelium Forum Index -> URL Rewriting
View previous topic :: View next topic  
Author Message
GigenFu
-


Joined: 07 Dec 2022
Posts: 2

PostPosted: Wed Dec 07, 2022 4:59 am    Post subject: How to hide extension like '.php' Reply with quote

Hi
As Title,I have a need to hide the php extension.
I want to convert like 'https://mysite/index.php?a=1' to 'http://mysite/index?a=1'

I checked the manual and the forum, and I know that the rewriting function is used, but I can’t find a way to simply hide the file extension.

Please Help,Thanks.
Back to top View user's profile Send private message
admin
Site Admin


Joined: 03 Mar 2002
Posts: 1295

PostPosted: Thu Dec 29, 2022 10:15 pm    Post subject: Re: How to hide extension like '.php' Reply with quote

GigenFu wrote:
Hi
As Title,I have a need to hide the php extension.
I want to convert like 'https://mysite/index.php?a=1' to 'http://mysite/index?a=1'

I checked the manual and the forum, and I know that the rewriting function is used, but I can’t find a way to simply hide the file extension.

Please Help,Thanks.


Have you checked the article at https://aprelium.com/abyssws/articles/urlrewriting-tutorial.html ? It explains a use case similar to yours and shows how to create a URL rewriting rule with such a capability.
_________________
Follow @abyssws on Twitter
Subscribe to our newsletter
_________________
Forum Administrator
Aprelium - https://aprelium.com
Back to top View user's profile Send private message
GigenFu
-


Joined: 07 Dec 2022
Posts: 2

PostPosted: Wed Jan 04, 2023 3:57 am    Post subject: Re: How to hide extension like '.php' Reply with quote

admin wrote:

Have you checked the article at https://aprelium.com/abyssws/articles/urlrewriting-tutorial.html ? It explains a use case similar to yours and shows how to create a URL rewriting rule with such a capability.



Yes, I have read this article.
The method of the article is
Quote:

'https://mysite/index.php?a=parameter1&b=parameter2' to 'http://mysite/index/parameter1/parameter2'

I also followed the instructions successfully.
But I have tested that if there are multiple folders, it will be wrong

I really want is
Quote:

'https://mysite/index.php?a=parameter1&b=parameter2' to 'http://mysite/index?a=parameter1&?b=parameter2'
or
'https://mysite/folder/index.php?a=parameter1&b=parameter2' to 'http://mysite/folder/index?a=parameter1&?b=parameter2'


My requirement is actually very simple, that is, to hide the extension file name.

Thanks for help
Back to top View user's profile Send private message
Horizon
-


Joined: 18 Feb 2022
Posts: 54

PostPosted: Sat Jan 07, 2023 5:11 am    Post subject: Reply with quote

Hello,
There's a way to do what you want.

But how do you know which links must be redirected to a .php file on your server?

WordPress for example does it by taking all Request URIs and redirecting them internally to its index.php file.

Then the WordPress index.php file has a well-defined set of Request URI patterns and which .php file to redirect them to.

Here I'm going to simply think that you have a dedicated host on your server where any file without extension should be redirected to a .php version if available.

The URLRewrite method:

Example URI:
Code:
/folder-1/folder-2/folder-3/script-file/?key-1=value-1&key-2=value-2

RegEx pattern:
Code:
^(?!.*?\.php(?:\/$|\/\?.*|\?.*|$))(.*?)(\/|)(\?.*|$)$

RegEx replacement (redirect to):
Code:
$1.php$2$3

End result (redirected to):
Code:
/folder-1/folder-2/folder-3/script-file.php/?key-1=value-1&key-2=value-2

* this RegEx pattern will silently ignore requests that already have a .php file extension.

How to:

Create one URLRewrite rule:

Do as shown above, relative to base for /.
Uncheck 'Apply to subrequests' and set the redirection to 'Internal redirect'.

The conditions should be REQUEST_FILENAME is not a file & is not a folder.
This is not case-sensitive.
-
However, how will you hide .php from your existing .php script redirects?

If you have a .php script that automatically does a 302 redirect with header("Location: https://mysite/another-script.php?key=value"), then you also have to modify your .php scripts to remove the '.php' text in any HTTP redirect you might find.
-
For handling 404 errors incase of non-existing files, you might want to use a custom 404 page (Error pages section of Abyss) to hide the .php extension from there as well so that it doesn't show up in visitor error pages.
Back to top View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> URL Rewriting All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group