.htaccess没有从网址

时间:2016-11-08 15:41:43

标签: php mysql apache .htaccess mod-rewrite

首先让我说我发现了很多脚本放入.htaccess文件中,我记下了所有按照我希望的方式工作的脚本。

因此,例如,如果我的htdocs文件夹中有一个名为test.php的文件,并且我访问了localhost / test.php,我希望在浏览器中将该文件重写为localhost / test。

我找到了2个.htaccess文件来执行此操作并在下面显示它们,但是它们的问题是每当我尝试使用表单将数据插入MYSQL数据库时我总是得到空白字段。

这是第一个

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

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

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

这是第二个

# Turn on the rewrite engine
RewriteEngine  on
# If the request doesn't end in .php (Case insensitive) continue processing rules
RewriteCond %{REQUEST_URI} !\.php$ [NC]
# If the request doesn't end in a slash continue processing the rules
RewriteCond %{REQUEST_URI} [^/]$
# Rewrite the request with a .php extension. L means this is the 'Last' rule
RewriteRule ^(.*)$ $1.php [L]

然后我发现一些脚本在搜索了几个小时和几个小时后就像一个魅力,然后昨天我去洗澡,回来后他们没有工作。 (我删除了旧的.htaccess文件,创建了一个新的文件,重新启动了mysql和apache,清除了浏览器数据等,而.htaccess仍然不能正常工作)并且他们在我去洗澡之前就按照我想要的方式工作了。

这两个之前有效(不,我没有同时将它们都放在.htaccess文件中)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]

第二个

# Options is required by Many Hosting
Options +MultiViews

RewriteEngine on

# For .php & .html URL's:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]

所以现在他们只能工作,如果我手动从网址中删除.php,但我不希望这样做,或者我不想手动更改我的php文档中的所有链接都没有.php扩展,我想使用其中任何一个,因为当我使用表单向MYSQL插入数据时,实际显示的数据和字段不是空白。

我还发现了一个添加尾部斜杠,因此localhost / test.php变为localhost / test /但是它工作了一次,现在它不再起作用,但如果我使用表单将数据插入到MYSQL数据库的字段也是空白的。

# Apache Rewrite Rules
 <IfModule mod_rewrite.c>
   Options +FollowSymLinks
   RewriteEngine On
   RewriteBase /

# Add trailing slash to url
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ $1/ [R=301,L]

# Remove .php-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^([^\.]+)/$ $1.php 

# End of Apache Rewrite Rules

就像我说的那样,我发现并记录了那些完全按照它们应有的方式,但现在它们没有工作,有人知道为什么吗?或者如何调整它们以完全符合我的要求?我在过去的一天里一直在寻找解决方案。顺便说一下,我在Windows 7上使用XAMPP

1 个答案:

答案 0 :(得分:0)

重写时,发送的URL不是用于调用脚本的URL,但您仍然可以访问原始URL。在index.php,在$_SERVER['REQUEST_URI']上展开?,第一项是您的路径,第二项(如果存在)parse_str($exploded[1], $_GET);