4

I tried a lot of code to remove .php from url
for example - ht.abuena.net/presto.php -> ht.abuena.net/presto
and vice versa - internally

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

nothing works - the url stays unchanged
page is reloading by right clicking on the button Reload and choosing - Empty Cache and Hard Reload - so I hope the cache is cleared

live example here - here

14
  • 1
    Why would the URL change? You don't do any redirection, only a rewrite. And why should it even change? The whole point is that you can have an URL like https://example.com/stuff internally served by /stuff.php (without the client even seeing any .php at all). I think you have it the wrong way round - just don't send people to /presto.php at all, link them to /presto instead... Commented Dec 19, 2021 at 18:18
  • @CherryDT - what if I have a folder named presto also ? Commented Dec 19, 2021 at 18:46
  • 1
    That would be a confusing semantical structure... Commented Dec 19, 2021 at 19:27
  • @CherryDT - ok suppose there is no such a folder. Are you sure that a link without file extension will always go to that file ? I think this is not a default, or regular browser's behaviour Commented Dec 19, 2021 at 19:32
  • No it's not default but it's what your .htaccess achieves that you showed above... Maybe I am misunderstanding the point here. I was thinking you want nice URLs without .php, so what you'd do is use the .htaccess setup that you posted above so that /abc will internally reach /abc.php. Anything else didn't make much sense to me. If that's not the case, can you explain better what you want to achieve here then, and why? Commented Dec 19, 2021 at 19:52

2 Answers 2

5

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

Please make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} ^$
##using THE_REQUEST variable for condition check.
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php/?\s [NC]
##Performing external redirect here.
RewriteRule ^  %1? [R=301,L]

##Performing rewrite for non-existing pages.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)/?$ /$1.php [QSA,L]
Sign up to request clarification or add additional context in comments.

13 Comments

thanks, url is rewriten but I'm getting error 404 - Not Found
@qadenza, could you please do let me know where is your htaccess present and where is your php file?
live address - ht.abuena.net/presto.php. htaccess file is in root and presto.php also
@qadenza, could you please try edited rules once? Please make sure to clear cache before testing your urls.
the same result. Pls try yourself on the given address
|
1

In my case if the project is core PHP one, I use the below lines in .htaccess file:

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

9 Comments

doesn't work for me. There is no redirection. And yes - it's a pure php site
Check in your server's httpd.conf file for AllowOverride entry. It should be AllowOverride All
I'm trying to find that file on server - in cpanel file manager - without success
@qadenza "There is no redirection." - That's correct. There shouldn't be a redirection under normal circumstances. You would only implement a redirect (which would be an additional rule) if the old URL had been indexed by search engines or linked to by external third parties. You should have already changed all your internal links (in your HTML source) to reference the extensionless URL - so under normal usage, no redirect is necessary.
@qadenza Not really, sorry. The Apache docs are my go-to, but they are rather lacking in examples. What you are dealing with here is mod_rewrite - which is just one of many Apache modules. It is one of the more complex (and powerful) Apache modules. See: httpd.apache.org/docs/2.4/rewrite
|

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.