[英].htaccess pretty URLs are not working
I'm trying to use pretty URLs on my site, and I don't know too much about htaccess, but after some search I ended up with this: 我试图在我的网站上使用漂亮的URL,但我对htaccess不太了解,但是经过一番搜索,我最终得到了这样的结果:
RewriteEngine On
RewriteRule ^([a-z]+)\/([0-9]+)\/?$ content.php?action=$1&id=$2 [NC]
I'm trying to change this: 我正在尝试更改此:
mysite.com/cms/content.php?action=content&id=80
mysite.com/cms/content.php?action=content&id=80
into: 成:
mysite.com/cms/content/80
mysite.com/cms/content/80
But somehow it's not working and what I get is a blank screen and a 404 error, 但是不知何故,我得到的是黑屏和404错误,
Failed to load resource: the server responded with a status of 404 (Not Found) --in-- /cms/files/plugin/tinymce/tinymce.min.js
About the 404 error: It should load a .js file from mystie.com/files/plugin/tinymce/tinymce.min.js -- not -- mystie.com/cms/files/plugin/tinymce/tinymce.min.js 关于404错误:应从mystie.com/files/plugin/tinymce/tinymce.min.js加载.js文件-不能-mystie.com/cms/files/plugin/tinymce/tinymce.min.js
My htaccess file is in the root folder. 我的htaccess文件在根文件夹中。 Am I doing something wrong?
难道我做错了什么?
I think the server did not read my .htaccess file. 我认为服务器未读取我的.htaccess文件。 How do I find out the issue?
我如何找出问题所在?
You have /cms/
in your URL before the rest, so you need to change RewriteRule
to something like this: 其余的URL之前都有
/cms/
,因此您需要将RewriteRule
更改为以下内容:
RewriteRule ^cms/([a-z]+)\/([0-9]+)\/?$ content.php?action=$1&id=$2 [NC]
Also, I hope you are aware that .htaccess can not work on some servers (like nginx or some custom ones), and even on Apache servers there can be mod_rewrite
disabled. 另外,我希望您知道.htaccess在某些服务器(例如nginx或某些自定义服务器)上无法运行,甚至在Apache服务器上也可以禁用
mod_rewrite
。
RewriteEngine On
RewriteRule ^/cms/([a-z]+)/([0-9]+)$ content.php?action=$1&id=$2 [NC]
Remove escape slashes \\ 删除转义斜线\\
My guess is that your scripts and styles are being linked to via relative URLs. 我的猜测是您的脚本和样式是通过相对URL链接的。 Because the old URL is
/cms/content.php
the URI base is /cms/
and your relative links all resolved to be under /cms/
, which I assume is what you want. 因为旧的URL是
/cms/content.php
所以URI的基础是/cms/
而您的相对链接都解析为/cms/
,我想这就是您想要的。
However, when you change your links to /cms/content/1234
, the URI base becomes /cms/content/
and all relative links will use this as its base, and since "content" isn't even a real folder, all those links will 404, and you won't load any of your CSS content or scripts. 但是,当您将链接更改为
/cms/content/1234
,URI基础将变为/cms/content/
并且所有相关链接均将其用作基础,并且由于“ content”甚至不是真正的文件夹,因此所有这些链接将显示404,并且您不会加载任何CSS内容或脚本。
Either change your links to absolute URLs or specify a URI base in the header of your pages (inside the <head> </head>
tags): 更改您到绝对URL的链接,或在页面标题(在
<head> </head>
标记内)指定URI基:
<base href="/cms/" />
You need to place this rule in /cms/.htaccess
: 您需要将此规则放在
/cms/.htaccess
:
RewriteEngine On
RewriteBase /cms/
RewriteRule ^([a-z]+)/([0-9]+)/?$ content.php?action=$1&id=$2 [NC,L,QSA]
Then you need add this in the <head>
section of your page's HTML: <base href="/" />
so that every relative URL is resolved from that URL and not the current page's relative URL. 然后,需要在页面HTML的
<head>
部分中添加此代码: <base href="/" />
以便从该URL解析每个相对URL,而不是从当前页面的相对URL解析。
You need to make sure to include the subfolder if your htaccess is in the root. 如果您的htaccess位于根目录中,则需要确保包含该子文件夹。 Try this rule.
试试这个规则。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cms/([a-z]+)/([0-9]+)/?$ /cms/content.php?action=$1&id=$2 [NC,L]
Also a blank screen usually means a PHP error. 另外,黑屏通常表示PHP错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.