网址重写
我正在使用 regx 进行 URL 重写。 创建一个 XML 文件并在 Global.ASAX 文件中写入以下代码
string sPath = Context.Request.Path;
Context.Items["VirtualURL"] = sPath;
Regex oReg;
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(Server.MapPath("~/Rule.xml"));
System.Xml.XmlElement _oRules = xmlDoc.DocumentElement;
foreach (System.Xml.XmlNode oNode in _oRules.SelectNodes("rule"))
{
oReg = new Regex(oNode.SelectSingleNode("url/text()").Value);
Match oMatch = oReg.Match(sPath);
if (oMatch.Success)
{
sPath = oReg.Replace(sPath,
oNode.SelectSingleNode("rewrite/text()").Value);
break;
}
}
Context.RewritePath(sPath);
虚拟 URL 为 /ProductDatabaseCMS/Category/Product/A320.aspx
原始 URL 为 /ProductDatabaseCMS/Product.aspx?PROD_ID=A320
Product.aspx 使用具有路径 /ProductDatabaseCMS/Main.master 的母版页,并通过路径 /ProductDatabaseCMS 包含样式表/App_Themes/Styles/Styles.css
当我尝试使用超链接控件通过其他网页打开product.aspx页面时,样式表在product.aspx页面中不起作用,因为它采用路径
/ProductDatabaseCMS/Category/Product/App_Themes/Styles/ Styles.css 和母版页中的所有链接也采用相同的路径
/ProductDatabaseCMS/Category/Product/...
我不想包含 /Category/Product/.. 因为这两个文件夹路径是虚拟的。
这个问题的解决方案是什么。
欺骗:URL重写
(答案:使用根相对路径)
I am using regx for URL rewriting. created a XML file and write the below code in Global.ASAX file
string sPath = Context.Request.Path;
Context.Items["VirtualURL"] = sPath;
Regex oReg;
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(Server.MapPath("~/Rule.xml"));
System.Xml.XmlElement _oRules = xmlDoc.DocumentElement;
foreach (System.Xml.XmlNode oNode in _oRules.SelectNodes("rule"))
{
oReg = new Regex(oNode.SelectSingleNode("url/text()").Value);
Match oMatch = oReg.Match(sPath);
if (oMatch.Success)
{
sPath = oReg.Replace(sPath,
oNode.SelectSingleNode("rewrite/text()").Value);
break;
}
}
Context.RewritePath(sPath);
virtual URL is /ProductDatabaseCMS/Category/Product/A320.aspx
original URL is /ProductDatabaseCMS/Product.aspx?PROD_ID=A320
product.aspx uses master page that has path /ProductDatabaseCMS/Main.master and it include style sheet via path /ProductDatabaseCMS/App_Themes/Styles/Styles.css
when i tried to open product.aspx page via other web page using hyperlink control then style sheet is not working in product.aspx page becuase it takes path
/ProductDatabaseCMS/Category/Product/App_Themes/Styles/Styles.css and all the links in master page also takes the same path
/ProductDatabaseCMS/Category/Product/...
i do not want to include /Category/Product/.. because these two folder path are virtual.
what is the solution for this.
dupe: URL Rewriting
(answer: use a root relative path)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请查看您的重写器配置/规则将有所帮助。
我怀疑您只需要停止处理重写规则顶部以 .css 结尾的任何 URL。
在不知道您使用哪个重写器的情况下,我无法给出任何示例。
It would help to see your config/rules for the rewriter please.
I suspect though that you just need to stop processing any URLs ending in .css at the top of your rewrite rules.
Without knowing which rewriter you are using I can't give any examples.
~/yourpathtoyourcss.css
tilda 将其削减
~/yourpathtoyourcss.css
tilda slash it up
我同意 teknohippy 的观点,图像也不起作用还是只是样式表? 我假设样式表没有在服务器上运行?
您可以使用在服务器上以 ~/ url 运行的 rel 标记来加载样式表,或者只是使重写规则仅适用于特定文件类型。
重写规则更改可能是最好的,但您可能会发现 rel 标记更快?
I agree with teknohippy, do images also not work or just stylesheets? I assume stylesheets aren't running at server?
You could either make stylesheets load using a rel tag that runs at the server with a ~/ url or just make your rewrite rules only applicable to specific file types.
The rewrite rule change is probably best but you may find rel tag is quicker?