Home > front end >  .htaccess Pretty URL with languages
.htaccess Pretty URL with languages

Time:03-15

I have a php website, loading the pages as below.

    if ( $secretfile && file_exists( $moddir . $name . "/" . $secretfile . ".php" ) ) {
  include_once( $moddir . $name . "/" . $secretfile . ".php" );
} else {
  if ( isset( $_GET[ 'mod' ] ) ? $_GET[ 'mod' ] : '' ) {
    $secretmodule = isset( $_GET[ 'mod' ] ) ? $_GET[ 'mod' ] : '';
    if ( $secretmodule && file_exists( $moddir . $name . "/page.php" ) ) {
      include_once( $moddir . $name . "/page.php" );
    } else {
      include_once( $moddir . "404.php" );
    }
  } else {
    include_once( $moddir . "homepage.php" );
  }
}

I am using htaccess to beautify the url as below.

RewriteRule ^([^/]*)/$ /index.php?mod=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?mod=$1&file=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ /index.php?mod=$1&file=$2&proid=$3 [L]

I want to add languages in session but i dont want to change the url of the main language (already indexed). only the other languages will be added an extra variable.

eg: 
main language(de) : www.site.com/module/file/     /index.php?mod=$1&file=$2 [L]
other lang (en) : www.site.com/en/module/file/    /index.php?lang=$1mod=$2&file=$3 [L]
other lang (fr) : www.site.com/fr/module/file/    /index.php?lang=$1mod=$2&file=$3 [L]

I am just not sure how to setup the htaccess for these configurations.

CodePudding user response:

With your shown attempts, please try following. Make sure your index.php file and htaccess both files are present in same folder(root) in your case.

Please make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
##Rules for en OR fr languages URLs to be rewritten in backend.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(en|fr)/([^/]*)/([^/]*)/?$ /index.php?lang=$1mod=$2&file=$3 [QSA,NC,L]

##Rules for links which are not started with en OR fr links.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ /index.php?lang=$1mod=$2&file=$3 [QSA,NC,L]

CodePudding user response:

You can have these set of rules in .htaccess:

RewriteEngine On

# ignore all files are directories from rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# rules with any lang component
RewriteRule ^(en|fr)/([^/]*)/$ index.php?lang=$1&mod=$2 [L,QSA]
RewriteRule ^(en|fr)/([^/]*)/([^/]*)/$ index.php?lang=$1&mod=$2&file=$3 [L,QSA]
RewriteRule ^(en|fr)/([^/]*)/([^/]*)/([^/]*)/$ index.php?lang=$1&mod=$2&file=$3&proid=$4 [L,QSA]

# rules without any lang component
RewriteRule ^([^/]*)/$ index.php?lang=de&mod=$1 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/$ index.php?lang=de&mod=$1&file=$2 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ index.php?lang=de&mod=$1&file=$2&proid=$3 [L,QSA]
  • Related