2016-05-17 133 views
1

因为我所有的文件都是PHP我已删除PHP扩展与此.htacess文件:删除目录名

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php 

所以我有这个网址:

http://example.com/live/kanal/some-title-1 
http://example.com/live/kanal/some-title-2 
http://example.com/live/kanal/some-title-13 
http://example.com/live/kanal/some-title-45 
http://example.com/live/kanal/2some-title-333 

等等

我想隐藏KANAL /从以下网址,获得网址是这样的:

http://example.com/live/some-title-1 
http://example.com/live/some-title-2 
http://example.com/live/some-title-13 
http://example.com/live/some-title-45 
http://example.com/live/2some-title-333 

如何使用mod_rewrite实现这一目标?

我使用在线生成器来完成重写规则,但是没有人可以从URL中删除一段文本。我也在这个论坛上搜索过,并且发现了一些关于mod重写目录的文章,但是当我粘贴我的.htacess代码时遇到了问题。

+0

在根中我有一个.htacess。在这个文件中,我只有404重定向。 在http://example.com/live/我有另一个,这是从帖子开始.htacess。 – Aleksandar

回答

2

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

RewriteEngine On 
RewriteBase /live/ 

RewriteRule ^(?!kanal/)(.*)$ kanal/$1 [L,NC] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.+?)/?$ $1.php [L] 

确保有在kanal/子目录中没有的.htaccess。

+0

很奇怪。我得到这个: Index of/live/kanal 以及这个目录中的所有文件都列出了.. – Aleksandar

+0

我的旧.htacess,我刚刚删除了.php扩展名,你可以在th post开始查看。现在我使用你的代码,并在活动文件夹中替换旧的.htacess。之后,我打电话给url http://example.com/live/并获取kanal /文件夹中的所有文件列表。 – Aleksandar

+0

这是正确的行为,因为它可以隐藏'kanal'文件夹。当您输入“http:// example.com/live/some-title-1”URL时会发生什么? – anubhava