Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 如果htaccess提供了特定URL,则重定向_Apache_.htaccess_Redirect_Webserver - Fatal编程技术网

Apache 如果htaccess提供了特定URL,则重定向

Apache 如果htaccess提供了特定URL,则重定向,apache,.htaccess,redirect,webserver,Apache,.htaccess,Redirect,Webserver,我有一个非常简单的url友好访问 RewriteEngine On RewriteBase / RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^(\w+)/?$ index.php?id=$1 所以domain.com/something被映射到domain.com/index.php?id=something 每个URL都可以有一个附加参数,例如domain.com/som

我有一个非常简单的url友好访问

RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^(\w+)/?$ index.php?id=$1
所以
domain.com/something
被映射到
domain.com/index.php?id=something

每个URL都可以有一个附加参数,例如
domain.com/something
可以采用
domain.com/something?自定义值的形式

现在我需要解决这个特殊情况:如果匹配了一个特定的ID,那么必须强制使用一个自定义值。因此,如果我去
domain.com/somethingelse
我想被重定向到
domain.com/somethingelse?自定义值

我尝试过不同的规则,但运气不好。这是我最后一次尝试,但我收到一条消息,服务器正在进行一次永远无法完成的重拨

RedirectMatch 301 ^/(somethingelse)/?$ /somethingelse?custom_value
另一次尝试是这样的,导致500内部服务器错误

RedirectMatch 301 ^/(somethingelse)/?$ somethingelse?custom_value
完全访问

RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RedirectMatch 301 ^/(somethingelse)/?$ somethingelse?custom_value
RewriteRule ^(\w+)/?$ index.php?id=$1
编辑1:

显然,这个规则在本地机器上有效(不总是),但没有在线服务器

RedirectMatch 301 ^somethingelse$ somethingelse?custom_value
RewriteRule ^/somethingelse$ /somethingelse?test [L,R=301]
编辑2:

我也尝试过(也在本地机器上工作,但没有在线服务器)

编辑3 以下是我的虚拟主机:

本地虚拟主机

<VirtualHost vhostname>
    DocumentRoot /var/www/path/to/project
    <Directory /var/www/path/to/project>
        AllowOverride All
    </Directory>
</VirtualHost>
<VirtualHost *:80>
    ServerName subdomain.domain.com
    DocumentRoot /var/www/path/to/project
    <Directory /var/www/path/to/project
        AllowOverride All
    </Directory>
</VirtualHost>

DocumentRoot/var/www/path/to/project
允许超越所有
服务器虚拟主机

<VirtualHost vhostname>
    DocumentRoot /var/www/path/to/project
    <Directory /var/www/path/to/project>
        AllowOverride All
    </Directory>
</VirtualHost>
<VirtualHost *:80>
    ServerName subdomain.domain.com
    DocumentRoot /var/www/path/to/project
    <Directory /var/www/path/to/project
        AllowOverride All
    </Directory>
</VirtualHost>

ServerName subdomain.domain.com
DocumentRoot/var/www/path/to/project

您可以在站点根目录中使用这些规则。htaccess:

RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^somethingelse/?$ %{REQUEST_URI}?custom_value [R=301,L,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\w+)/?$ index.php?id=$1 [L,QSA]

请确保在新浏览器中进行测试,或在完全清除浏览器缓存后进行测试。

您希望向客户端或浏览器显示的URL是什么?您将在此处获得无休止的重定向,因为只有URL的路径组件与您的模式匹配-所以下次
/somethingelse?自定义值仍然匹配
^/(somethingelse)/?$
。尝试将RewriteCond和-Rule组合起来,类似于
RewriteCond%{QUERY\u STRING}^自定义值$
重写规则^somethingelse/?$/somethingelse?自定义值[R=301,L]
@anubhava如果客户端键入
domain.com/anything
domain.com/anything?自定义值
,则我将保持原样。但是,如果客户端类型为
domain.com/specificmatch
,则必须将其重定向到
domain.com/specificmatch?自定义值
@misorude,我稍后再试