Apache htaccess:将路径参数(国家代码)添加到URL&;服务索引.html

Apache htaccess:将路径参数(国家代码)添加到URL&;服务索引.html,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我想通过htaccess向Angular应用程序添加国家代码“en gb”。文件夹“en gb”不存在。我只希望它显示在url&SERVICE index.html中 当我点击它时,应该将url从服务器上的根文件夹更改为&SERVICE index.html(而不在url中显示实际的index.html文件) 如果请求的文件/文件夹不存在,例如,url应从服务器上的根文件夹(/index.html)更改为&service index.html 如果存在文件/文件夹,例如。-url不应更改&

我想通过htaccess向Angular应用程序添加国家代码“en gb”。文件夹“en gb”不存在。我只希望它显示在url&SERVICE index.html中

  • 当我点击它时,应该将url从服务器上的根文件夹更改为&SERVICE index.html(而不在url中显示实际的index.html文件)

  • 如果请求的文件/文件夹不存在,例如,url应从服务器上的根文件夹(/index.html)更改为&service index.html

  • 如果存在文件/文件夹,例如。-url不应更改&文件应相对于服务器上的根文件夹(/vendor.js)提供


  • 这也适用于目录。例如,应该在服务器上提供/css/style.css

My.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
   
    #does the folder AND file exist? (ie. /index.html & css/style.css)
    #yes - serve it & pop out of htaccess
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
    RewriteRule ^ - [L]

    #does the url already have a country flag set?
    #no - add country flag (ie. http://angular.wlocal/ -> http://angular.wlocal/en-gb)
    RewriteCond %{REQUEST_URI} !/en-gb
    RewriteRule ^(.*)$ en-gb/$1
    
    #if file folder not found, serve index.html (ie. http://angular.wlocal/en-gb/this-does-not-exist -> should serve index.html)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.html [L]

</IfModule>

<VirtualHost 192.168.56.1:80>
    DocumentRoot "C:\Users\Dev\Documents\angular-10\dist"
    ServerName angular.wlocal
    ServerAlias angular.wlocal
    RemoteIPHeader X-Forwarded-For

    <Directory "C:\Users\Dev\Documents\angular-10\dist">
        DirectoryIndex index.html
    AllowOverride ALL
    Require all granted
    </Directory>
</VirtualHost>
目前,URL没有更改为/en gb-我认为这是因为
重写了规则。index.html[L]
将其删除

如何从根目录提供index.html,同时保留此/en gb重写

。。。。是否保留此/en gb重写

如果您想更改URL,则需要外部“重定向”,而不是“重写”

因为同一个请求不能映射到文件和目录,所以它目前没有任何作用。这些条件需要改变。在本例中,测试
请求\u FILENAME
比连接两个服务器变量更简单

请尝试以下操作:

RewriteEngine On

#does the folder OR file exist? (ie. /index.html & css/style.css)
#yes - pop out of htaccess and allow Appache to serve it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

#does the url already have a country flag set?
#no - add country flag (ie. http://angular.wlocal/ -> http://angular.wlocal/en-gb)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule !^en-gb/ /en-gb%{REQUEST_URI} [R=302,L]

#if file folder not found, serve index.html (ie. http://angular.wlocal/en-gb/this-does-not-exist -> should serve index.html)
RewriteRule . index.html [L]
/en gb
前面加前缀。。。通过在
RewriteRule
模式中执行此检查,并在替换字符串中使用
REQUEST\u URI
服务器变量而不是
$1
反向引用,可以避免检查其前缀是否为
/en gb
。对
REDIRECT\u STATUS
环境变量的检查可以确保我们只处理直接请求,而不是对
index.html
的重写请求

客户端发出初始请求时,
REDIRECT\u STATUS
env var为空(未设置)。但是,当请求被重写为
index.html
(根据下面的规则)时,该环境变量将更新为
200
(与200 OK HTTP状态相同)。因此,通过检查这个env变量是否为空(即,
^$
),我们可以避免为内部重写处理规则(在这种情况下,这将导致无休止的循环)。另一种方法是在下面的重写中简单地使用
END
标志(Apache 2.4+),以防止重写引擎循环,但这可能需要在将来的其他重写中添加,并且Apache版本实际上没有在问题中说明

请注意,这是一个302(临时)重定向。如果这是永久性的,那么将其更改为301-但只有在您确认其正常工作后。否则,最终可能会出现缓存问题

无需重新检查请求是否未映射到上一个规则中的文件或目录,因为第一个规则已执行此检查

此处不需要
包装,应将其删除

如果我删除下面的内容,url会更改,但是


如果你删除了最后一条规则,那么你的angular应用程序将永远不会被调用,因此你将获得404。但是,简单地删除最后一条规则不应导致URL更改,因为之前的规则仍然是重写,并且这里没有其他“重定向”?

“这也应适用于目录。例如
http://angular.wlocal/css/style.css
“-吹毛求疵,但那是一个文件,不是一个目录。想必您的内部链接已经包含此
en gb
国家/语言代码?这是一个很好的解决方案,谢谢您的分享。关于
RewriteCond%{ENV:REDIRECT_STATUS}^$
如果我没有错(我对htaccess知之甚少),这是一个环境值,我们正在检查它吗?如果您能在同一方面提供更多指导,我们将非常感谢您,谢谢您。@RavinderSingh13谢谢。是,
REDIRECT\u STATUS
是一个环境变量(与服务器变量相反)。我更新了我的答案来解释更多。另一种方法是检查\u请求,但我发现在大多数情况下,当您只需要防止重定向/重写循环时,
重定向\u状态
更容易/更简单。
#does the folder AND file exist? (ie. /index.html & css/style.css)
#yes - serve it & pop out of htaccess
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteEngine On

#does the folder OR file exist? (ie. /index.html & css/style.css)
#yes - pop out of htaccess and allow Appache to serve it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

#does the url already have a country flag set?
#no - add country flag (ie. http://angular.wlocal/ -> http://angular.wlocal/en-gb)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule !^en-gb/ /en-gb%{REQUEST_URI} [R=302,L]

#if file folder not found, serve index.html (ie. http://angular.wlocal/en-gb/this-does-not-exist -> should serve index.html)
RewriteRule . index.html [L]