使用htaccess隐藏URL中的目录

时间:2015-05-13 20:37:43

标签: php regex apache .htaccess mod-rewrite

我正在尝试删除php扩展以及在我的域中使用.htaccess文件隐藏子目录。但是,我对正则表达式知之甚少,我真的被困在这里。如果有人可以帮忙,我真的很感激!

我想要实现的目标是:
www.example.com/index.php访问www.example.com/index
www.example.com/assets/php/company.php至www.example.com/company

CURRENT .htaccess FILE

Options +FollowSymLinks -MultiViews

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /
RewriteRule ^assets/php/(.*)$ /$1 [L,NC,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

#To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

</IfModule>

目前,我可以成功从网址中删除.php扩展名以及实现http://www.example.com/company,但它会显示404 Not Found错误。我相信我错过了RewriteCond系列,但我不知道如何编写它。或者我在/ assets / php /?

中需要另一个htaccess文件

如果有人能帮忙解决这个问题,我们真的很感激!不过,谢谢你的阅读。

干杯,

TY

2 个答案:

答案 0 :(得分:1)

你可以在root .htaccess中使用这样的规则:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /assets/php/([^.]+)\.php [NC]
RewriteRule ^ /%1 [L,NC,R=301]

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

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

答案 1 :(得分:0)

使用此

RewriteEngine on
RewriteBase /

#enforce www subdomain
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^sitename.com [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#forward all requests, except new/admin, to the 'new' directory without the user's awareness
RewriteRule  new/admin  -  [S=2]
RewriteRule  ^$ new/    [L]
RewriteRule  (.*) new/$1 [L]