Mobile app version of vmapp.org
Login or Join
Nimeshi995

: Rewrite url - remove trailing / if any and add .php I have a simple question, yet I've found no posts that answer my question. I need an htaccess file that rewrites the urls so that it

@Nimeshi995

Posted in: #Htaccess #ModRewrite #Url #UrlRewriting

I have a simple question, yet I've found no posts that answer my question. I need an htaccess file that rewrites the urls so that it removes the ending / if any, and adds .php no matter what the folder it is in. Essentially, this is what I want to do:
example.com/login/ -> example.com/login.php http://example.com/login -> example.com/login.php http://example.com/user/test/login/ -> example.com/user/test/login.php http://example.com/user/test/login -> example.com/user/test/login.php http://example.com///// -> example.com////.php http://example.com//// -> example.com////.php http://example.com//// ... // -> example.com//// ... /.php


I want it to rewrite the URL to remove the trailing slash if any, and add .php (but do this ONLY if the file exists). And it needs to have 'wildcard' support, meaning it works for all files without having to copy the code for each individual file. That way, the URLs 'look clean'.

I've found many posts with sample .htaccess files similar to what I want. But I haven't found one without one of these problems:


It doesn't use 'wildcard' rewrites, so I would have to copy the code for each file to make it rewrite the URL
It removes the trailing slash if any, then appends the filename to /index.php?file= instead of rewriting to the file.
The 'wildcard' rewrite works only in the root directory. I can't use it if the files are nested like so: /////.


Is this possible? I haven't seen anyone do it before.

Edit: I'll award a bounty if someone comes up with an .htaccess file that does all of this correctly, but also supports URL parameters.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Nimeshi995

1 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

I assume you want to remove the trailing slash (if any) with an external redirect in order to canonicalise the URL and then append the .php extension with an internal rewrite (ie. hidden from the user) in order to correctly route the URL. This is a two-stage process.

NB: The trailing slash should remain if accessing a physical directory.

Using mod_rewrite in .htaccess:

RewriteEngine On

# Remove the trailing slash (if any) for non-directories
# This is essentially unconditional in order to canonicalise the URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ / [R=302,L]

# Append ".php" if that file would exist (internal rewrite / hidden from user)
RewriteCond %{DOCUMENT_ROOT}/.php -f
RewriteRule (.*) .php [L]


Change the R=302 (ie. temporary redirect) to R=301 to make it permanent. 302s are easier to test with as they aren't cached by the browser. (Make sure the browser cache is clear before testing.)

It is "wildcard" in the sense that it works for any file depth. .* simply grabs everything.

URL parameters will be appended onto the substitution by default - nothing special you need to do in this respect.





UPDATE: Since the canonical URL would seem to be the URL with the trailing slash (to which requests are being POST'd) then the above should be changed to the following:

RewriteEngine On

# Append .php if that file exists (internal rewrite)
RewriteCond %{DOCUMENT_ROOT}/.php -f
RewriteRule (.*?)/?$ .php [L]


This will accept requests for both /path/to/file and /path/to/file/ and internally rewrite the request to /path/to/file.php. You will need to handle the URL canonicalisation in your script. If all your URLs already point to /path/to/file/ (ie. with a trailing slash) then you can modify the RewriteRule pattern and remove both ? ie. (.*)/$ - this makes the trailing slash mandatory on the request.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme