htaccess RewriteRule to shorten the URLs


htaccess RewriteRule to shorten the URLs

我想找到一个能做到这一点的RewriteRule

mysite.net/jqMAS/mysite.net/jqMAS=>mysite.net/index.php?id=jqMAS

我使用了这样一个.htaccess文件:

RewriteEngine On
RewriteRule ^(.*) index.php?id=$1

但不幸的是,它不起作用(也许mysite.net/index.php本身被重定向到mysite.net/index.php?index.php,等等?):调用mysite.net/jqMAS会产生500 Internal Server Error

我们应该使用什么RewriteRule来缩短URL


以下是index.php页面(我没有提到标题)的样子:

  <body>
  Bonjour <?php echo $_GET['id']; ?>
  </body>

试试下面的.htaccess文件,因为它是我自己网站上成功使用的设置。

# Enable the rewriting engine.
RewriteEngine On
# Change requests for a shorter URL
# Requires one or more characters to follow the domain name to be redirected
RewriteRule     ^([A-Za-z0-9-]+)/?$     index.php?id=$1             [L] 

有两个htaccess文件:将htaccess文件保存在应用程序文件夹中,如下所示:拒绝所有人。

并将以下代码粘贴到应用程序文件夹外的htaccess文件中:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

它非常适合我。

您需要RewriteCond来停止对真实文件和目录的重写:

RewriteEngine On
# if request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-d
# if request is not for a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?id=$1 [L,QSA]