2016-04-11 211 views
3

当用户输入URL像htaccess的重写规则

http://example.com/app/abcd123/ 

我想从

http://example.com/app/index.php?param=abcd123 

显示姬网页,不必更改浏览器的URL。

我把.htaccess文件的应用程序文件夹中的代码

RewriteEngine on 
RewriteCond %{REQUEST_URI} ^/index.php [NC] 
RewriteCond %{QUERY_STRING} ^param=(.*) 
RewriteRule (.*) http://example.com/app/%1? [R=301,L] 
+0

是'/ app' /一个真正的目录? – anubhava

+0

是的,我把我的htaccess文件 – moonvader

回答

3

可以在/app/.htaccess使用此代码:

RewriteEngine on 
RewriteBase /app/ 

# external redirect from actual URL to pretty one 
RewriteCond %{THE_REQUEST} /index\.php\?param=([^\s&]+) [NC] 
RewriteRule^%1? [R=302,L,NE] 

# internal forward from pretty URL to actual one 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.+?)/?$ index.php?param=$1 [L,QSA] 
+1

谢谢 - 此代码按预期工作! – moonvader

1

试试这个.htaccess代码。它应该帮助你。

确保重写基地应该是当前目录的根目录。

RewriteBase /app/ 

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-s 
RewriteRule ^(.*)$ index.php?param=$1 [QSA,NC,L] 

RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^(.*)$ index.php [QSA,NC,L] 

RewriteCond %{REQUEST_FILENAME} -s 
RewriteRule ^(.*)$ index.php [QSA,NC,L] 

在此,如果用户输入等http://example.com/app/qwerty呼叫将被像http://example.com/app/index.php?params=qwerty

处理这应该是工作,我测试它。让我知道你是否面临任何麻烦。

+1

谢谢 - 代码作品和我唯一面临的问题 - 是我的字体停滞加载,因为他们也在这个文件夹中,并有像“/app/fonts/font.ttf ?ver = 1.4.4“ – moonvader

+1

@moonvader,anubhava的回答对你的情况来说是完美的。 – Sibidharan

0
RewriteEngine On 
RewriteBase /app 
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /index.php?param=$1 [L] 

检查它有:http://htaccess.mwl.be/或其他在线htaccess的测试服务