Mod_rewrite apache for custom flat links stuck

I want to create flat links for my site. The .htaccess code .htaccess as follows:

 <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 site is working, and it can be seen through print_r($_GET) and get all the values.

My site has this code: <img src="images/icons/image.png" /> , where the image is on the path: /images/icons/up.png .

Now I can visit my site as: http://somedomain.com/home and everything works fine. But when I put: http://somedomain.com/home/ , it got stuck.

The image gets this path 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 .

How to solve this?

It would be helpful if there was any solution by changing .htaccess , since I want to use relative paths for all links, not the full path.


Thanks Ben Clifford for your help. According to him, if I redirect the second rule to a format similar to the first, and not to index.php. Then it should work.

+4
source share
1 answer

You can change this second rewrite rule to redirect the user to a non-version of the path, and not to index.php; then when this redirected page is loaded, it will fall into the first rule with the correct base path.

The problem is that the image name replaces everything after the last / in the URL: when you say somedomain.com/home, then the image file name is added to somedomain.com/, but in the second case final / after the house. Using a rewrite rule to crop this on / off will fix this, I think.

+1
source

Source: https://habr.com/ru/post/1412831/


All Articles