【问题标题】:PHP, .htaccess apache unified mod_rewrite just with one variablePHP、.htaccess apache 统一 mod_rewrite 只需一个变量
【发布时间】:2012-10-23 07:20:04
【问题描述】:

我对 .htaccess 和 mod_rewrite 还是很陌生。我可以准备带有特定网址的 seo 友好网址。但是,我想制作一个为我统一一切的。

例如;

Original : http://www.domain.com/index.php?module=profile&id=1.
SEO : http://www.domain.com/profile/1
.htaccess Code: RewriteRule ^profile/(.*)  ?module=profile&id=$1 [L]

如您所见,我必须在 .htaccess 文件中指定 profile 和任何其他模块名称

我想做的是;

Original : http://www.domain.com/?request=module/profile/id/1/other/any
SEO : http://www.domain.com/module/profile/id/1/other/any

但是 mod_rewrite 的相同逻辑不适用。

RewriteRule ^(.*)  ?request=$1 [L]

有没有办法解决这个问题?

我的完整 .htaccess;

# Disable server signature
ServerSignature Off
####################################################################
# MOD REWRITE
####################################################################
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

RewriteRule (.*)  /index.php?request=$1 [L]

</IfModule>

我可以毫无问题地这样使用它;

# Disable server signature
ServerSignature Off
####################################################################
# MOD REWRITE
####################################################################
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

RewriteRule ^/ index.php [L]

RewriteRule ^content/(.*) ?module=content&request=$1
</IfModule>

工作版本

# Disable server signature
ServerSignature Off
####################################################################
# MOD REWRITE
####################################################################
<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteBase /

RewriteRule (.*) index.php?request=$1 [L]
</IfModule>

但是我仍然需要指定至少一件事。

感谢您的帮助。

【问题讨论】:

    标签: php apache mod-rewrite seo


    【解决方案1】:

    我认为你的方法没有任何问题,你只需要一个小小的修复:

    编辑 2

    RewriteRule (.*)  index.php?request=$1 [L]
    

    就是这样。顺便提一句。您不妨将所有内容重定向到 index.php 并在 $_SERVER['REQUEST_URI'] 超全局中查找请求参数。

    编辑

    问题出在这里:

    <IfModule mod_deflate.c>        <<<<<
    SetOutputFilter DEFLATE
    # Don't compress
    SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
    #Dealing with proxy servers
    <IfModule mod_headers.c>        <<<<<
    Header append Vary User-Agent
    </IfModule>
    </IfModule>                     <<<<<
    

    编辑 3

    ...
    RewriteEngine on
    
    # Pass any request not referring directly to a file or directory in the
    # filesystem to index.php    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) index.php?request=$1 [L]
    

    【讨论】:

    • 感谢您的快速回复aefxx。但是,我收到 500 Internal Server 错误。我添加了完整的 .htaccess 文件。也许我在这条规则之前做错了什么。如果您能指出正确的方向,我会很高兴。我不想进入超全球,但感谢您提供的信息。
    • 可能是最后一个&lt;/IfModule&gt; 放错了位置,请尝试将其粘贴到# GZIP 评论之前。
    • 我注意到的其他一点:RewriteRule / index.php [L] 规则是多余的,因为这是由 mod_dir 处理的(很可能开箱即用)。相关指令应为DirectoryIndex index.php [...],其中“...”可以是您喜欢的任何后备。
    • 我已经更新了我当前的 .htaccess 文件。这次我收到Object not found! 错误。 Mod重写是严重的颈部疼痛:(
    • 我已经删除了除服务器签名和 mod-rewrite 规则之外的所有内容。我仍然收到Object not found! 错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 2016-01-19
    • 2010-09-20
    相关资源
    最近更新 更多