Ad

How Can I Redirect Unstructured Link Like (https://example.com/digital-marketing.php/ercbierubcrei) Using .htaccess?

- 1 answer

There is a issue of unstructured link like (https://example.com/digital-marketing.php/ercbierubcrei)

How can I redirect its to main root?

My .htaccess not working, I'm using below code in .htaccess:-

RewriteEngine On
ErrorDocument 404 /example.com
Ad

Answer

There is a issue of unstructured link like (https://example.com/digital-marketing.php/ercbierubcrei)

How can I redirect its to main root?

I assume /digital-marketing.php is a file on your filesystem. In which case /ercbierubcrei is additional pathname information (or path-info). By default, the PHP handler allows path-info, so /digital-marketing.php is still served but at the different URL (if that's what you mean by "unstructured link"?).

You can simply disable path-info on these URLs by setting the following at the top of your .htaccess file:

AcceptPathInfo Off

Now, any URL that contains path-info will trigger a 404 instead and your custom 404 error document will be called. (You can still override this behaviour with mod_rewrite if you wish.)

Alternatively, if you wanted to redirect/digital-marketing.php/ercbierubcrei to /digital-marketing.php (is that what you mean by "redirect its to main root"?*1) then you can do something like the following near the top of your .htaccess file using mod_rewrite:

RewriteEngine On
RewriteRule ^(.+?\.php)/ /$1 [R=302,L]

This redirects a URL of the form /<something>.php/<anything> to /<something.php, effectively discarding the path-info portion of the URL.

(*1 - If by "main root" you do literally mean the document root of the site then that is not recommended. Google would likely treat this as a soft-404 anyway. A custom 404 with a meaningful message would be preferable.)

ErrorDocument 404 /example.com

This does not look "valid". The 2nd argument to the ErrorDocument directive should ideally be a local URL-path to the document that handles the error response.

For example:

ErrorDocument 404 /error-documents/my-404-error-document.php
Ad
source: stackoverflow.com
Ad