Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php .htaccess用于url重写的代码_Php_.htaccess_Url Rewriting - Fatal编程技术网

Php .htaccess用于url重写的代码

Php .htaccess用于url重写的代码,php,.htaccess,url-rewriting,Php,.htaccess,Url Rewriting,我想在我的网站中使用url重写,如下所示: 当用户尝试访问 http://www.mysite.com/sectionname/ 要进行处理,它必须转到文件as http://www.mysite.com/handler.php?sect=sectionname 但在地址栏中,用户只能看到第一个url 我使用了以下代码,但它不起作用 Options +FollowSymlinks RewriteEngine on RewriteRule ^([^/]+)$ sectionthumbs.php

我想在我的网站中使用url重写,如下所示: 当用户尝试访问

http://www.mysite.com/sectionname/
要进行处理,它必须转到文件as

http://www.mysite.com/handler.php?sect=sectionname
但在地址栏中,用户只能看到第一个url

我使用了以下代码,但它不起作用

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^/]+)$ sectionthumbs.php?sect=$1
但它不起作用。请告诉我哪里错了。
提前感谢……

类似的方法应该会奏效:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /handler.php?sect=$1 [L,QSA]
正如@Repox所说

但是,我在过去的所有项目中都使用了类似的方法:

RewriteRule ^([^/\.]+)/$ index.php?module=$1 [L]
但那是因为我使用MVC

另一个小技巧是,假设您将一个变量传递到查询字符串中,为了使它看起来漂亮,您可以使用如下内容:

RewriteRule ^media/([^/\.]+)/?$ index.php?media=$1 [L]

使用类似于MVC框架的CodeIgniter,您也可以使用我的建议,不同之处在于请求将指向index.php/$1。然后,框架会找出哪个控制器(如果愿意的话是模块)、哪个控制器方法,如果需要,还会将任何参数传递给该方法。通过应用你的建议,重写似乎不那么灵活;但是,由于Codeignator和大多数MVC框架都是自动完成这项工作的,我认为最初的问题是他想通过开发自己的“框架”来完成这项工作,如果你愿意的话,这就是为什么我提供了这个。干杯