English 中文(简体)
删除 PHP 扩展名并用.htaccess 重写查询字符串
原标题:Removing PHP extension and re-writing query string with .htaccess

我试图调整现有网站的结构,同时将URL重写为更方便用户的格式。我现在拥有的是一个 http://example.com/test_vars.php?test_var1=value1&test_var2=value2 ,理想的情况是,我要的是 http://example.com/test_vars/test_var1/value1/t_var2/value2

诚然,我刚开始修改Htaccess文件,但做了一些研究并尝试了几件事情。 我最接近我所寻找的东西(当然不是那么近),但我附上了以下代码:

Options +FollowSymLinks
Options +Indexes
RewriteEngine on
DirectoryIndex index.html

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

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.html
RewriteRule .* index.html

有了这个,我最后有了一个 http://example.com/test_vars.php/test_var1/value1/test_var2/value2 的 URL,但是,没有一个变量被传送到页面上。

最佳回答

在您的 test_vars.php 中,您必须手动分析参数。 $_SERVER[ REQUEST_URI] 将包含 /test_var1/value1/test_var2/value2 。 例如,写一个简单的函数,用 explode() 解析字符串。

要在不使用“.php”扩展名的情况下使用您的 URL,请更改此行

RewriteRule ^(([^/]+/)*[^.]+)$ /$1 [L]

为此:

RewriteRule ^test_vars test_vars.php

现在您有了您漂亮的 URL : < a href=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

问题回答

暂无回答




相关问题
URL rewrite in IIS6 - Third party tools

I have collected the following api/sdk/whatever which provide modules for doing url rewriting in IIS 5/6/7. Some are free-open source and some require $. I haven t tried any of them. I would like to ...

Rewrite spring-security redirect URLs

I m trying to get Tuckey UrlRewriteFilter to tidy up URLs for my webapp. One problem I ve got is that when spring-security notices that an anonymous user is trying to access a protected resource it ...

ASP.NET Friendly URLs

In my research, I found 2 ways to do them. Both required modifications to the Application_BeginRequest procedure in the Global.Asax, where you would run your code to do the actual URL mapping (mine ...

Could I get by HttpModule the URL that contains * before?

My web-site is working under ASP.NET 3.5, IIS7, Integrated pipiline mode. My HttpModule is getting request to all resources, including URLs like this: http://my-site.com/address And I would support *...

Domains & Foreward Slash

This is rather difficult to explain so please bear with me. We will be hosting 4 websites on our server and the plan is to have each site sit under its own domain: site-a.com site-b.com sub1.site-b....

URL Mod-Rewrite

I currently have a working URL: http://example.com/security-services.php?service=fixed-camera-surveillance and then I have PHP say, $_REQUEST[ service ] to do some stuff... But I d like to ...

热门标签