简体   繁体   English

htaccess友好的URL重写规则

[英]htaccess friendly URL rewrite rule

I have recently change my hosting and i need a htaccess rewrite rule for my files. 我最近更改了主机,我需要为文件设置htaccess重写规则。 I tried many examples but no one really works for my case. 我尝试了许多示例,但没有人真正适合我的情况。 I have never been really good in htaccess and on my older hosting i didn't really need anything it just worked but here is not. 我从来都不是htaccess的佼佼者,在我的旧主机上,我真的不需要任何可以正常使用的东西,但是这里不是。 Basically i want that my PHP files are without extensions and treated like a directory. 基本上,我希望我的PHP文件没有扩展名,并像目录一样对待。 So for example i have a URLs like these: 因此,例如,我有这样的网址:

www.domain.com/file1/{id}/{nick} www.domain.com/file1/{id}/{nick}

So for example: 因此,例如:

www.domain.com/myfile1/104/username www.domain.com/myfile1/104/用户名

www.domain.com/myotherfile/455/nick www.domain.com/myotherfile/455/nick

File1 in this case is a PHP file and {id} and {nick} are changable. 在这种情况下,File1是一个PHP文件,并且{id}和{nick}是可更改的。 I have this structure on my entire site for many other PHP files etc. So if possible i want one universal rule for all files . 我在整个站点上都有许多其他PHP文件等的这种结构。因此,如果可能的话,我希望所有文件都具有一个通用规则 I tried with htaccess to remove php extenstion etc but all I got is 404 error. 我尝试使用htaccess删除php扩展等,但我得到的只是404错误。 Also URL in browser should stay friendly without PHP extension. 另外,在没有PHP扩展名的情况下,浏览器中的URL应该保持友好。 So in my case if i rewrite my URL manually in: www.domain.com/file1.php/{id}/{nick} it worked but i don't want to change all the links etc on my website. 因此,就我而言,如果我手动在以下网址中重写我的URL:www.domain.com/file1.php/{id}/{nick},它可以工作,但我不想更改网站上的所有链接等。 So all i want is to hide PHP extension and treat PHP files as directory. 所以我只想隐藏PHP扩展名并将PHP文件视为目录。

Thanks 谢谢

You can use this single and generic front controller rule in site root .htaccess: 您可以在站点根目录.htaccess中使用此单一且通用的前端控制器规则:

AcceptPathInfo On
DirectoryIndex index.php

RewriteEngine On
RewriteBase /

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM