简体   繁体   中英

Flat URL Rewriting using .htaccess

I am trying to shorten my following URL

From

 www.domain.com/index_1.php?var=value
 www.domain.com/index_2.php?name=value

To

 www.domain.com/value
 www.domain.com/name/value

what i really what is,

 www.domian.com                    // This should reach index_1.php
 www.domain.com/value              // This should reach index_1.php
 www.domain.com/name               // This should reach index_2.php
 www.domain.com/name/value         // This should reach index_2.php
 www.domain.com/name/              // The last slash should be omitted and redirect to "www.domain.com/name"

The "value" range may contains [ 0-9 ][ AZ ][ az ][ - ] [ _ ]

I have tried following .htaccess code,

 Options +FollowSymlinks
 RewriteEngine On
 RewriteBase /
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule (^name?$)|(^name/([^/]+)/?$) /index_2.php?name=$1 [L]
 RewriteRule ^([^/][^(name)]+)/?$ /index_1.php?var=$1 [L]

when i type

 www.domian.com                    // It Works.
 www.domain.com/value              // It shows "404 Page Not Found".
 www.domain.com/name               // It works.
 www.domain.com/name/value         // It works.
 www.domain.com/name/              // It shows "404 Page Not Found".

if it can be done using PHP, please Answer.

You have to distinguish between www.domain.com/name and www.domain.com/value .

otherwise, you can done this by using PHP.

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

# For www.domain.com/value
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?var=$1 [L,QSA]

# For www.domain.com/name/value
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([\w-]+)/([\w-]+)/?$ index_2.php?$1=$2 [L,QSA]

In index.php:

if(isset($_GET["var"])) {
  $value = $_GET["var"];
  if($value == "name"){
    include"index_2.php";  
  }else{
    include"index_1.php";
  }
}else{
  include"index_1.php";
}

You can use:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

# For www.domain.com/value
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index_1.php?var=$1 [L,QSA]

# For www.domain.com/name/value
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ index_2.php?$1=$2 [L,QSA]

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