【问题标题】:.htaccess modrewrite breaks my forms.htaccess modrewrite 破坏了我的表单
【发布时间】:2012-09-11 14:48:59
【问题描述】:

我在我的 .htaccess 中添加了一个 modrewrite,现在我的所有表单都不会将发布数据发送到 php 脚本。

代码: 例如:我的 .htaccess 删除 .php 扩展名,如果是目录,则删除尾部斜杠。

RewriteEngine On

# Redirect index to site root
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,NC]
RewriteRule ^index.php$ http://www.mysite.com/ [R=301,L]

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.mysite.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.mysite.com/$1 [QSA,R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [QSA,L]

有没有办法在 modrewrite 规则中添加例外,从而忽略某些文件/目录? 所以所有名为 form-process.php、email-process.php、upload-process.php 的文件, save-process.php 可以忽略吗?

编辑:我刚刚读到我可以将 !POST 添加到我的查询中。如果我这样添加,它会起作用吗?

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/ !POST
RewriteRule ^(.+)\.php$ http://www.mysite.com/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f !POST
RewriteRule ^([^/.]+)$ $1.php [L]

编辑:还在上面的规则中添加了 [QSA],但仍然没有变量传递给我的表单

编辑:对于其他遇到此问题的人,我通过选择发布数据的文件并将它们从对 .htaccess 文件的更改中删除来修复它。这并不理想,但现在可以使用。

# ignore recipes page
RewriteRule ^directorytoremove - [L,NC]
RewriteRule ^filenametoremove - [L,NC]

重定向 301 不会通过 POST 传递任何数据,即使您添加了 QSA。所以你需要做一个post-redirect-get 例如你的表单发布数据,被重定向并且接收文件获取数据并处理它。如果您遇到同样的问题,请将上面的代码粘贴到您的 .htaccess 文件顶部,并在打开的 RewriteEngine On 语句下方。

希望对遇到同样问题的人有所帮助!

【问题讨论】:

    标签: php html .htaccess mod-rewrite


    【解决方案1】:

    你可以在开头添加这样的规则(就在RewriteEngine on之后)

    RewriteRule ^(form|email|upload|save)-process\.php$ - [L]
    

    【讨论】:

    • 谢谢,这给了我所选页面的 404 错误。这是否意味着以下规则仍在处理中?我认为 [L] 阻止了它的进一步处理?
    • [L] 停止处理当前规则之后的规则,如果您仍然收到 404,请尝试清除浏览器缓存并发布您编辑的代码(确保文件也确实存在)
    • 我已经使用RewriteRule ^directoryname - [L,NC] 从重写中删除目录取得了一些成功。我想知道我是否可以对上面的单个文件执行相同的操作。谢谢亚兹马特! :)
    • 当然可以,我敢肯定这只是一个简单的东西,就是搞乱并产生404。
    • 这些文件在根目录中吗?和 htaccess 它在哪里?
    【解决方案2】:

    试试这个:

    # Resolve .php file for extensionless php urls
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^([^/.]+)$ $1.php [QSA,L]
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多