从网址中删除目录名称 [英] Remove directory name from url
问题描述
因为我所有的文件都是php,所以我已使用此.htacess文件删除了php扩展名:
Because all my files are php I have removed php extension with this .htacess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
所以我有这个URL:
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
等...
我想从url中隐藏kanal/以获得这样的URL:
I want to hide kanal/ from url, to get URL like this:
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实现此目标?
How can I achieve this using mod_rewrite?
我使用在线生成器来执行重写规则,但是没有一个可以从URL中删除一段文本.我也在这个论坛上进行了搜索,发现了一些关于mod rewrite目录的帖子,但是在粘贴.htacess代码时遇到了问题.
I used online generators for doing the rewrite rules but none can remove a piece of text from the URL. Also I made search on this forum, and I found some posts about mod rewrite directory but I have issues when pasting in my .htacess code.
推荐答案
您可以在 /live/.htaccess
中使用此代码:
You can use this code in /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.
Make sure there is no .htaccess in kanal/
sub-directory.
这篇关于从网址中删除目录名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!