2014-09-20 177 views
0

我想找到一个RewriteRule做这个:htaccess的重写规则,缩短的URL

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.p hp等?):致电mysite.net/jqMAS产生500 Internal Server Error

什么RewriteRule应该用来做这种URL缩短?


这里是什么index.php页面(我没提头)的样子:

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

'它不工作'什么不行? – Qix 2014-09-20 23:47:29

+0

@Qix:调用mysite.net/jqMAS会产生500内部服务器错误。 – Basj 2014-09-20 23:52:32

+2

@Basj apache错误日志说什么? – 2014-09-20 23:58:44

回答

1

有2个htaccess的文件: 保持你的htaccess文件应用程序文件夹中的: 所有拒绝。

并粘贴下面的代码到你的htaccess文件的应用程序文件夹之外:

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

这是为我工作完美。

+0

谢谢!我仍然得到'500内部服务器错误'......我不知道为什么。 2 .htaccess文件怎么样?我只有一个。你能否提供一些关于“全部拒绝”的细节?你谈论文件权限吗?我应该为.htaccess和index.php设置什么文件权限? 644? – Basj 2014-09-21 08:54:40

+0

你是否在localhost或服务器链接上发生此错误? – nagesh29 2014-09-21 09:04:10

+0

我不知道原因。我通过将所有文件设置为权限644并最终使用: ''IfModule mod_rewrite.c>','RewriteEngine On','RewriteBase /','RewriteCond%{REQUEST_FILENAME}!-f','RewriteCond%{REQUEST_FILENAME} !-d','RewriteRule ^(。*)$ /index.php?id=$1 [L]','' – Basj 2014-09-21 09:07:11

1

试试下面的.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] 
1

你需要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]