PHP:htaccess 默认变量
我想为 url 末尾的查询设置默认变量
.htaccess 文件按如下方式重定向 url:
http://www.server.com/folder/subfolder/index.php?page="some-page-name"
显示 url
http://www.server.com/folder/some-page-name
如果未设置页面名称,则
http://www.server.com/folder/
,如下所示:默认值为“index” ”。我可以使用 php 函数 header("location:url")
但如果我不想要的 url... 则会在末尾显示“index”部分。
访问内容
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^.*\.css.*$ [NC]
RewriteRule ^(.*)$ subfolder/index.php/?page=$1 [L]
</IfModule>
<IfModule mod_rewrite.c>
ErrorDocument 404 /error.html
ErrorDocument 403 /error.html
</IfModule>
I want to set a default variable for a query that comes at the end of a url
The .htaccess file redirects the url as follows:
http://www.server.com/folder/subfolder/index.php?page="some-page-name"
displayed url
http://www.server.com/folder/some-page-name
if the page name is not set, as in:
http://www.server.com/folder/
the default would be "index". I could use php function header("location:url")
but that would display the "index" part at the end if the url... that I don't want.
htacess content
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^.*\.css.*$ [NC]
RewriteRule ^(.*)$ subfolder/index.php/?page=$1 [L]
</IfModule>
<IfModule mod_rewrite.c>
ErrorDocument 404 /error.html
ErrorDocument 403 /error.html
</IfModule>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不必重定向到index.php。您可以使用类似的内容:
如果您只想 http://example.com/folder/ 显示您的索引页面,您可以在 PHP 脚本中使用以下内容:
You don't have to redirect to index.php. You can use something like:
If you just want http://example.com/folder/ show up your index page, you could use the following in your PHP script:
在您的 PHP 文件中尝试以下代码:
这会将
$pageID
设置为默认值“index”。然后,如果 mod_rewrite 给出了不同的 PageID,则$pageID
将设置为该值。然后,您的代码应使用$pageID
的值。Try this code in your PHP file:
What this will do is set
$pageID
to a default value of "index". Then, if a different PageID is given by the mod_rewrite,$pageID
will bet set to that value. Your code should then use the value of$pageID
.您的问题太长,没有阅读,但是如果您只想在 .htaccess 文件中设置一个变量并将其传递给 PHP,那么这样做很容易 -
在 .htaccess 文件中(http://httpd .apache.org/docs/2.0/mod/mod_env.html#setenv):
在你的 PHP 脚本中 (http://www.php.net/manual/en/reserved.variables.environment.php):
或者,如果你有
ErrorDocument
处理程序,您也许也可以简单地发送状态代码(请参阅header()
的第三个选项,http://us.php.net/manual/en/function.header.php)。Your question was too long, didn't read, however if you just want to set a variable in your .htaccess file and pass it to PHP it's pretty easy to do it like this -
In the .htaccess file (http://httpd.apache.org/docs/2.0/mod/mod_env.html#setenv):
In your PHP script (http://www.php.net/manual/en/reserved.variables.environment.php):
Alternately, if you have
ErrorDocument
handlers, you might be able to simply send a status code as well (see the third option toheader()
, http://us.php.net/manual/en/function.header.php).