简体   繁体   中英

Php custom website redirecting .php urls to rewrites urls

What is the best way to redirect.PHP URLs to rewrites URLs. The rewrites URLs is working perfect. I want if user come or enter URL like /index.php or cart.php etc.. it should redirect to domain.com or /cart.

This is my .htaccess code below for rewriting URLs

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
RewriteCond %{HTTP_HOST} baqsa\.pk [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.baqsa.pk/$1 [R,L]
</IfModule>

I am trying to figure out and redirect it to rewrite URLs but facing a issue. I am creating this below code.

if(empty($params))
{
    $request_uri = parse_url($_SERVER['REQUEST_URI']);
    $uri_to = basename($request_uri["path"],'.php');

    if($uri_to == 'index')
    {
        header('Location:'.BASE_URL, true, 301);
    }
    elseif($request_uri["path"])
    {
        header('Location:'.$uri_to, true, 301);
    }
}

But it is also redirecting the ajax.PHP files like /load.php to /load so then the ajax is not working.

I just ignore the load.php file and it is working now.

elseif($request_uri["path"] && $uri_to != 'load'){}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM