我有一个修改 example.com/en/about/topic 的 mod_rewrite 规则至 example.com/en/view.php?p=pages/about/topic.php
我的规则是:
RewriteRule ^en/([a-zA-Z0-9/]+)$ en/view.php?p=pages/$1.php
view.php 有我所有的固定内容,如菜单、 Logo 等,它使用 php include 来获取 pages/topic.php
适用于 example.com/en/topic -> example.com/en/view.php?p=pages/topic.php但是当我尝试 example.com/en/about/topic -> example.com/en/view.php?p=pages/about/topic.php , 它找到 view.php和 pages/about/topic.php并加载它们,但尝试从 en/about/... 加载我所有的 css 文件和图像等而不是 en/...它对 view.php 和 topic.php 中的图像执行此操作。最令人沮丧的是,如果我在规则中添加 [R],一切正常,但它违背了 SEO 练习的目的!
我一整天都在做这个,但运气不好。虽然我确实发现了 mod_rewrite 日志记录和一些 handy guides .
这是我的 example.com/en/test 日志中的内容(有效):
[perdir C:/tt/xampp/htdocs/] rewrite 'en/test' -> 'en/view.php?p=pages/test.php'
[perdir C:/tt/xampp/htdocs/] strip document_root prefix: C:/tt/xampp/htdocs/en/view.php -> /en/view.php
[perdir C:/tt/xampp/htdocs/] internal redirect with /en/view.php [INTERNAL REDIRECT]
[perdir C:/tt/xampp/htdocs/] pass through C:/tt/xampp/htdocs/en/view.php
[perdir C:/tt/xampp/htdocs/] pass through C:/tt/xampp/htdocs/en/base/code/view.css
还有 example.com/en/about/test(没有)
[perdir C:/tt/xampp/htdocs/] rewrite 'en/about/dir/test' -> 'en/view.php?p=pages/about/dir/test.php'
[perdir C:/tt/xampp/htdocs/] strip document_root prefix: C:/tt/xampp/htdocs/en/view.php -> /en/view.php
[perdir C:/tt/xampp/htdocs/] internal redirect with /en/view.php [INTERNAL REDIRECT]
[perdir C:/tt/xampp/htdocs/] pass through C:/tt/xampp/htdocs/en/view.php
[perdir C:/tt/xampp/htdocs/] pass through C:/tt/xampp/htdocs/en/about
如有任何帮助,我们将不胜感激!
最佳答案
在某种程度上你问错了问题。您给出的规则有效并按照您的意愿行事。您的问题是脚本正在为您的 CSS、图像等发布相对地址,因此当客户端浏览器请求 example.com/en/about/topic 时其中包含一个相关链接 href="base/code/view.css" ,客户端会将其转换为
example.com/en/about/base/code/view.css
鉴于您不想更改脚本,因此您的挑战是识别这种类型的模式并去除 SEO 编码引入的无关目录,所以如果这种情况是 example.com/en/<don't care>/base/code/...那么以下类型的规则将执行所需的操作
RewriteRule en/.+?/base/code/(.*) en/base/code/$1 [L]
替换/base/code通过您需要的任何匹配模式。
关于html - SEO mod_rewrite 内部 uri 目录问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8876243/
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.