pretty的网址与htaccess的 [英] Pretty URLs with htaccess
问题描述
可能重复:
htaccess的重写打破相对路径
我希望有人能帮助我。我试图把我的网站上使用pretty的网址。
目前,我有哪些隐藏我的包含所有我的PHP文件夹的子域,以及一个命令来更改从http我的协议HTTPS,反之亦然一些htaccess的命令。在pretty的URL被证明是困难的。我想通过htaccess的,而不是单纯的PHP做。我设法写它的工作,但在那之后无我的CSS或JavaScript工作的命令。
#重定向从website.com到www.website.com
的RewriteCond%{HTTP_HOST} ^ website.com [NC]
的RewriteCond%{HTTP_HOST}!^ WWW [NC]
(。*)重写规则^ $ HTTP://www.% {HTTP_HOST} / $ 1 [R = 301,L]
#重定向URL到www
的RewriteCond%{THE_REQUEST} ^ [AZ] {3,9} \ / defaultfolder /([^ \] +)\ HTTP /
重写规则^(。*)$ defaultfolder / error.php [L]
#这个部分隐藏defaultfolder,试图修改此code在这里进行友好的URL。
的RewriteCond%{HTTP_HOST} www.website.com
的RewriteCond%{REQUEST_URI}!defaultfolder /
的RewriteCond%{REQUEST_URI}!图片/
重写规则^(。*)\ / $ defaultfolder / $ 1 \ .PHP [L]
#https重定向
的RewriteCond%{} SERVER_PORT 80
的RewriteCond%{REQUEST_URI}(/defaultfolder/)(login|securepages|pages)\.php
重写规则^ defaultfolder /(.*)$ https://www.website.com/$1 [R,L]
#http重定向
的RewriteCond%{} SERVER_PORT 443
的RewriteCond%{REQUEST_URI}(/ defaultfolder /)(指数|不安全页)。\ PHP
重写规则^ defaultfolder /(.*)$ http://www.website.com/$1 [R,L]
之后,非我的CSS或JavaScript的工作。
这可能是一个相对/绝对路径的问题。当您更改URL路径,相对链接是要打破由于基数发生了变化。您可以添加几个规则,对CSS / JS请求指向正确的地方,或者尝试使用添加正确的基地<基地>
中的头标记您的网页。是这样的:
<基地的HREF =/>
否则,你可能需要重写CSS方式和js得到解决。例如说,你有你的所有样式 / CSS /
和你的所有脚本 / JS /
:
的RewriteCond%{REQUEST_URI}!^ / CSS /
重写规则^(。* \。CSS)$ / CSS / $ 1 [L]
的RewriteCond%{REQUEST_URI}!^ / JS /
重写规则^(。* \。JS)$ / JS / $ 1 [L]
< HEAD><链接相对=样式类型=文/ CSS的href =CSS /的main.css媒体=所有/>
和,这是在主子目录
因此,这将意味着你需要这样的规则:
的RewriteCond%{REQUEST_URI}!/ defaultfolder /
重写规则^(CSS | JS)/ \(*)。(CSS | JS)$ defaultfolder /%1 /%2%3 [L]
Possible Duplicate:
htaccess rewrite breaks relative paths
I am hoping someone can help me. I am trying to set my website to use pretty URLs.
I currently have a few htaccess commands which hide my subdomain folder containing all my php files as well as a command to change my protocol from http to https and vice versa. The pretty url is proving to be difficult. I want to do it solely by htaccess and not php. I managed to write a command which worked but non of my css or javascript worked after that.
# Redirect from website.com to www.website.com
RewriteCond %{HTTP_HOST} ^website.com [NC]
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# redirect url to www
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /defaultfolder/([^\ ]+)\ HTTP/
RewriteRule ^(.*)$ defaultfolder/error.php [L]
#this section hides the defaultfolder, tried to modify this code here to make friendly urls.
RewriteCond %{HTTP_HOST} www.website.com
RewriteCond %{REQUEST_URI} !defaultfolder/
RewriteCond %{REQUEST_URI} !images/
RewriteRule ^(.*)\/$ defaultfolder/$1\.php [L]
#https redirect
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} (/defaultfolder/)(login|securepages|pages)\.php
RewriteRule ^defaultfolder/(.*)$ https://www.website.com/$1 [R,L]
#http redirect
RewriteCond %{SERVER_PORT} 443
RewriteCond %{REQUEST_URI} (/defaultfolder/)(index|nonsecure pages)\.php
RewriteRule ^defaultfolder/(.*)$ http://www.website.com/$1 [R,L]
non of my css or javascript worked after that.
This is probably a relative/absolute path issue. When you change the path of the URL, relative links are going to break because the base has changed. You can either add a couple of rules to point requests for css/js to the right places, or try adding the correct base using the <base>
tag in the header of your pages. Something like:
<base href="/">
Otherwise you may have to rewrite the way css and js get resolved. Say for instance, you have all of your styles in /css/
and all your scripts in /js/
:
RewriteCond %{REQUEST_URI} !^/css/
RewriteRule ^(.*\.css)$ /css/$1 [L]
RewriteCond %{REQUEST_URI} !^/js/
RewriteRule ^(.*\.js)$ /js/$1 [L]
<head><link rel="stylesheet" type="text/css" href="css/main.css" media="all"/>
and this is in the main subfolder
So that would mean you need a rule like this:
RewriteCond %{REQUEST_URI} !/defaultfolder/
RewriteRule ^(css|js)/(.*)\.(css|js)$ defaultfolder/%1/%2.%3 [L]
这篇关于pretty的网址与htaccess的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!