I want to create flat links for my website. The .htaccess
code is following:
我想为我的网站创建平面链接。 .htaccess代码如下:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9A-Za-z]+)?$ /index.php?page=$1 [L]
RewriteRule ^([0-9A-Za-z]+)/?$ /index.php?page=$1 [L]
</IfModule>`
Now my website is working which can be seen through print_r($_GET)
and getting all the values.
现在我的网站正在运行,可以通过print_r($ _ GET)看到并获取所有值。
My website has a code like this: <img src="images/icons/image.png" />
, where the image resides on path: /images/icons/up.png
.
我的网站有这样的代码:,其中图像位于路径:/images/icons/up.png。
Now I can visit my website as: http://somedomain.com/home
and everything works fine. But when I put: http://somedomain.com/home/
, it gets stuck.
现在我可以访问我的网站:http://somedomain.com/home,一切正常。但是,当我提出:http://somedomain.com/home/时,它会卡住。
The image gets a path like this for the browser: http://somedomain.com/home/images/icons/image.png
which is not available and it should be: http://somedomain.com/images/icons/image.png
.
该图像为浏览器提供了这样的路径:http://somedomain.com/home/images/icons/image.png这是不可用的,它应该是:http://somedomain.com/images/icons/image巴纽。
How to solve this?
怎么解决这个?
It would helpful if there is any solution by modifying the .htaccess as I want to use relative paths for all links and not full path.
如果通过修改.htaccess有任何解决方案会有用,因为我想使用所有链接的相对路径而不是完整路径。
Thanks to Ben Clifford for your help. According to him if I redirect the second rule one to the format like the first one and not over index.php. Then it should work.
感谢Ben Clifford的帮助。根据他的说法,如果我将第二个规则重定向到第一个规则的格式而不是index.php。然后它应该工作。
1 个解决方案
#1
1
You could change that second rewrite rule to redirect the user to the non-/ version of the path, rather than to index.php; then when that redirected page is loaded it would hit the first rule, with the correct base path.
您可以更改第二个重写规则,将用户重定向到路径的非/版本,而不是index.php;然后,当加载重定向的页面时,它将使用正确的基本路径命中第一个规则。
The issue is that the image name replaces everything after the last / in the URL: when you say somedomain.com/home then the image filename is appended to somedomain.com/ but in the second case, the final / is after the home. Using a rewrite rule to prune this / off would fix that, i think.
问题是图像名称会替换URL中最后一个/之后的所有内容:当您说somedomain.com/home时,图像文件名将附加到somedomain.com/但在第二种情况下,最终/位于主页之后。我认为使用重写规则来修剪这个/关闭会解决这个问题。
#1
1
You could change that second rewrite rule to redirect the user to the non-/ version of the path, rather than to index.php; then when that redirected page is loaded it would hit the first rule, with the correct base path.
您可以更改第二个重写规则,将用户重定向到路径的非/版本,而不是index.php;然后,当加载重定向的页面时,它将使用正确的基本路径命中第一个规则。
The issue is that the image name replaces everything after the last / in the URL: when you say somedomain.com/home then the image filename is appended to somedomain.com/ but in the second case, the final / is after the home. Using a rewrite rule to prune this / off would fix that, i think.
问题是图像名称会替换URL中最后一个/之后的所有内容:当您说somedomain.com/home时,图像文件名将附加到somedomain.com/但在第二种情况下,最终/位于主页之后。我认为使用重写规则来修剪这个/关闭会解决这个问题。