2015-10-04 49 views
1

我使用RewriteRule来创建SEO友好的网址。创建规则后,在错误的目录中查找外部文件。.htaccess重写加载错误目录中的文件

.htaccess文件代码:

# 1 -- Error 404 -- 
ErrorDocument 404 /NotFound.php 

#2 -- IGNORE INDEX -- 
IndexIgnore * 
#3 -- Default charter set -- 
AddDefaultCharset UTF-8 

RewriteEngine On  
RewriteRule ^questions/([0-9]+) questions.php?id=$1 [NC,L] 

通常从css/filename.css加载CSS。 应用重写规则后,从​​

加载文件如何创建一个RewriteRule它只是改变URL行为,以及如何强制questions.php?id=34&title=somethingquestions/34/something自动?

+3

使用在'questions.php'了''元素。 – hjpotter92

回答

2

对于css问题,只需在你的css,js,images文件中使用绝对路径而不是相对路径。这意味着您必须确保这些文件的路径始于http://或斜杠/

否则您可以添加此仅低于<head>您网页的HTML部分:<base href="/" />让每一位相对URL是从基本URL解决,而不是从当前页面的URL。

对于第二个问题,您需要重定向规则将旧URL重定向到漂亮链接。

有这样说:

# 1 -- Error 404 -- 
ErrorDocument 404 /NotFound.php 

#2 -- IGNORE INDEX -- 
IndexIgnore * 
#3 -- Default charter set -- 
AddDefaultCharset UTF-8 
Options -MultiViews 
RewriteEngine On 

# external redirect from actual URL to pretty one 
RewriteCond %{THE_REQUEST} /questions\.php\?id=([^\s&]+)&title=([^\s&]+)\s [NC] 
RewriteRule^/questions/%1/%2? [R=302,L,NE] 

RewriteCond %{THE_REQUEST} /questions\.php\?id=([^\s&]+)\s [NC] 
RewriteRule^/questions/%1? [R=302,L,NE] 

# internal forward from pretty URL to actual one 
RewriteRule ^questions/([0-9]+)/?$ questions.php?id=$1 [NC,L,QSA] 

RewriteRule ^questions/([0-9]+)/([\w-]+)/? questions.php?id=$1&title=$2 [NC,L,QSA]