evenloooo
evenloooo

Reputation: 77

Htaccess - How to remove path without query string after last slash on url

I'd like to remove path without query string after last slash on url.

For example, there is a url: https://www.example.com/example.php/fakepath/fakepath2/fakepath3?id=123

I want to add a rewrite rule in .htaccess file, and redirect origin url to this url: https://www.example.com/example.php?id=123

Becasue I found this issue:

This is a correct url: http://www.twhappy.com/index.php?action=blog&category=6

If I add slash after index.php, the url will be http://www.twhappy.com/index.php/?action=blog&category=6

If I add fake path after index.php:

http://www.twhappy.com/index.php/?action=blog&category=6 http://www.twhappy.com/index.php/fakepath/?action=blog&category=6 http://www.twhappy.com/index.php/fakepath/fakepath2/fakepath3/?action=blog&category=6

These url can be visit after I added fake path.

So, I want a rewrite rule to redirect them to correct url.

Is there have any idea for this case?

Thanks you so much.

Upvotes: 0

Views: 414

Answers (2)

Phil
Phil

Reputation: 165069

UPDATED ANSWER

Here's a RedirectMatch rule from mod_alias that should strip any PATH_INFO from .php files. This should perform the redirect you're after

# .htaccess
RedirectMatch permanent "^(.*\.php)/.*" "$1"

Old answer(s) below

You certainly don't need mod_rewrite for this. Instead, enable AcceptPathInfo, eg

# .htaccess
AcceptPathInfo On

Then, requests for /example.php/fakepath/fakepath2/fakepath3?id=123 will resolve to example.php with

var_dump($_GET);
var_dump($_SERVER['PATH_INFO']);

producing

array(1) {
  ["id"]=>
  int(123)
}
string(19) "/fakepath/fakepath2/fakepath3"

Alternatively, if you don't want to support URLs with PATH_INFO, you can turn it off with

# .htaccess
AcceptPathInfo Off

then requests for URLs that contain PATH_INFO will result in a 404.

Upvotes: 1

Black Mamba
Black Mamba

Reputation: 15625

Please try:

RewriteEngine On
RewriteRule ^example.php example.php/fakepath/fakepath2/fakepath3

Upvotes: 0

Related Questions