Remove index.php?route= from OpenCart

家住魔仙堡 提交于 2019-12-08 00:37:29
pratik vegad

Try This Its working for me using .htaccess.

RewriteCond %{THE_REQUEST} \ /index\.php\?_route_=?([^&\ ]*)
RewriteRule ^ /%1? [L,R]

Just try this..

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 

Old question i know but maybe someone would find it helpful: The reason SEO urls break if you change the HTACCESS rule is that they depend on _route_ for clarification that "yes this is a request for a clean url, no its not a standard route".

If you change index.php?_route_= in the HTACCESS you must also change the _route_ IF conditionals in /catalog/controller/common/seo_url.php. It may get heavily wonked out though if you do this, try it on test stores first.

Nothing should be seeing the _route_ except you in the logs or things like analytics logs since its a masked utility uri. If you see real humans are asking what the link is, then take action. Otherwise, its best to leave it.

Also have the same problem and any that I found not solved my problem. But! I remember that I also working with Laravel and in this engine well working url path. Don't ask why I associated OpenCart with Laravel. So I enable SEO radio button in System -> Settings -> Server you also can disable it I don't see difference in the site work.

This solution consist from several part:

1. In your root folder replace .htaccess file content with next shapshot:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?route=$1 [L]
</IfModule>

2. Go to /system/library/url.php and find link method and replace it with next:

public function link($route, $args = '', $secure = false) {
  if ($this->ssl && $secure) {
    $url = $this->ssl . 'index.php?route=' . $route;
  } else {
    $url = $this->url . 'index.php?route=' . $route;
  }

  if ($args) {
    if (is_array($args)) {
      $url .= '&amp;' . http_build_query($args);
    } else {
      $url .= str_replace('&', '&amp;', '&' . ltrim($args, '&'));
    }
  }

  foreach ($this->rewrite as $rewrite) {
    $url = $rewrite->rewrite($url);
  }

  $url = str_replace('index.php?route=', '', $url);

  return $url; 
}

Since you changed .htaccess in root folder and change url generating method you also need remember about admin part. If you what see pretty url in admin just copy .htaccess from root folder to /admin folder.

If you want save orignal url in admin side you must little bit change link method with next:

public function link($route, $args = '', $secure = false) {
  if ($this->ssl && $secure) {
    $url = $this->ssl . 'index.php?route=' . $route;
  } else {
    $url = $this->url . 'index.php?route=' . $route;
  }

  if ($args) {
    if (is_array($args)) {
      $url .= '&amp;' . http_build_query($args);
    } else {
      $url .= str_replace('&', '&amp;', '&' . ltrim($args, '&'));
    }
  }

  foreach ($this->rewrite as $rewrite) {
    $url = $rewrite->rewrite($url);
  }

  //  Skip admin path
  if (strpos($_SERVER['REQUEST_URI'], '/admin') !== 0) {
    $url = str_replace('index.php?route=', '', $url);
  }

  return $url; 
}

Result:

Before

http://YOUR_SITE/index.php?route=product/product&product_id=52

After

http://YOUR_SITE/product/product&product_id=52

Before

http://YOUR_SITE/index.php?route=account/login

After

http://YOUR_SITE/account/login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!