2014-09-20 220 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]