Home » Php » How can i redirect unstructured link like (https://example.com/digital-marketing.php/ercbierubcrei) using .htaccess?

How can i redirect unstructured link like (https://example.com/digital-marketing.php/ercbierubcrei) using .htaccess?

Posted by: admin February 25, 2020 Leave a comment

Questions:

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
How to&Answers:

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

Answer:

A simple 301 redirect will do the job.

redirect 301 /old_url http://www.domainroot.com/

Answer:

use Complete url of your site. Like this

ErrorDocument 404 https://www.yourdomain.com/example.php

Answer:

Please refer to below .htaccess examples:

Redirect http://yourdomain/digital-marketing.php/apple to http://yourdomain/:

RewriteEngine On
RewriteCond %{REQUEST_URI} /digital-marketing.php/*
RewriteRule ^ / [L,R=301]

Redirect http://yourdomain/digital-marketing.php/orange to http://yourdomain/ref?code=orange:

RewriteEngine On
RewriteCond %{REQUEST_URI} /digital-marketing.php/*
RewriteRule ^digital-marketing.php/(.*)$ /ref?code=$1 [L,R=301]