1

So let's say I have a url

https://website.com/file.php?test

I'd like for this to be rewritten to

https://website.com/file/test

I've tried using many solutions on the web but none work on my site for whatever reason.

My current HT access file is:

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]

This code successfully removes the .php extension so that I am able to have urls like website.com/file?test and not file.php?test.

treyBake
  • 6,440
  • 6
  • 26
  • 57
Savish Khan
  • 53
  • 1
  • 1
  • 8
  • You can fetch some parameters, however, consider a query string like `?id=1&category=2&name=foo&surname=bar` from e.g. a form. Parameters could occur in different orders. You could even do a recursion in mod_rewrite, however, this would result in bad performance. Consider to redirect from PHP instead since you can build a URL in one step and perform validations much easier. There is also a [FallbackResource](https://httpd.apache.org/docs/trunk/mod/mod_dir.html#fallbackresource) directive. With this you can avoid mod_rewrite at all. – Pinke Helga Aug 09 '19 at 22:14
  • Can you elaborate on this? This is usually done the other way around, when file.php already exists and you want to 'prettify' your urls. See [here](https://stackoverflow.com/questions/16388959). – msg Aug 09 '19 at 22:23
  • @msg According to code (except there is no captured `\?.*` in the `${THE_REQUEST}` condition) the term "I'd like for this to be rewritten to" seems to be wrong. Probably "redirect to" is meant. This can be senseful when there are old bookmarks or plain HTML forms shell be redirected to a "nice" URL. – Pinke Helga Aug 09 '19 at 22:26
  • @Quasimodo'sclone Might be, but "so that I am able to have urls without the extension" and the fact that the first rule redirects removes the extension and the second rewrites to the original location looks odd to me. Only reason I can see is to not touch the codebase and update all urls. – msg Aug 09 '19 at 22:38
  • Possible duplicate of [Rewrite Rule in htaccess convert query string into slashes php](https://stackoverflow.com/questions/43468946/rewrite-rule-in-htaccess-convert-query-string-into-slashes-php) – treyBake Aug 09 '19 at 22:43

1 Answers1

1

You may use these rules in your site root .htaccess:

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php\?([^&\s]+)\s [NC]
RewriteRule ^ %1/%2 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R=301,L]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?$2 [L,QSA]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This doesn't work for me. I tried changing my url from website.com/file.php?test to website.com/file/test and it did not function. – Savish Khan Aug 15 '19 at 00:58