简体   繁体   中英

How to rewrite URLs in php5?

Hi I want to rewrite my site URLs. Now I am using PHP 5 for development. I tried with the following .htaccess.

RewriteEngine on

RewriteRule   ^(.*)/(.*).html$ $1.php?code=$2 [L]
RewriteRule   ^(.*).html$ $1.php [L]

The URL without argument is working, but the first one shows some problems. It redirects to the page, but the page styles are missing whatever value I pass for code. Why is it happening? Is there any option for rewriting URLs using PHP5?

If you redirect to 'folder/file.html' all of your relative links will be messed up. It will be looking for 'css/style.css' in 'folder/css/style.css' instead.

You can either use absolute links ie 'www.example.com/css/style.css' or rewrite your link paths as well.

RewriteRule   ^(.+)/css/(.+)$ css/$2 [L]

If you talk about css styling, I think it might be because you're entering a new subdirectory, and the style.css isn't there but in the main directory. It's about relative paths, try using a absolute path ("www.web.com/style.css")

You have your URL in $_SERVER['REQUEST_URI'] which you can parse in your php script and include required file. In your example 'REQUEST_URI' will be '/folder/file.html'. So you can leave only this RewriteRule ^(.*).html$ $1.php

在html头中,您在包含CSS,JS等之前,先设置<base href="http://mysite.com/"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM