.htacces mod_rewrite路径错误

时间:2018-04-02 14:53:07

标签: php .htaccess

我的网站目前在PHP上运行,带有

等直接链接
www.example.com/index.php?go=somewhere

我喜欢构建干净的喜欢,不显示该页面是php并且看起来像ordonary html链接,为此我使用.htaccess文件跟随他的内容

RewriteEngine on
RewriteRule    ^exampleCategory/([A-Za-z0-9-]+)/?$    index.php?go=$1    [NC,L] 
除了像这样的人之外我是

example.com/exampleCategory/item7

成为

example.com/index.php?go=item7

部分有效,但似乎index.php使用给定路径而不是服务器根路径

当index.php加载项目网站时,没有css且没有图片显示,可能是因为图片链接看起来像这样

example.com/exampleCategory/item7/images/item7/item7_1.jpg

而不是

example.com/images/item7/item7_1.jpg

如果我通过php链接调用该站点,它可以正常工作。

可能是什么问题/如何解决这个问题? 由于类别将是动态和可扩展的,因此无法创建与给定路径匹配的服务器路径。我想有一个中央图像文件夹。

1 个答案:

答案 0 :(得分:0)

查看上面给出的详细信息,您似乎没有排除目录和文件。

此代码在运行WAMP的Windows中进行测试。如果有任何问题在评论部分写下来。

class PageScraperController < ApplicationController
  require 'nokogiri'
  require 'open-uri'
  require 'diffy'
  require 'htmlentities'
  require 'uri'
  require 'sidekiq-scheduler'

  def scrape
    @url = watched_link_params[:url].to_s
    puts "LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOG #{@url}"
    @page = Nokogiri::HTML(open(@url))
    coder = HTMLEntities.new
    @encodedHTML = coder.encode(@page)
    create(@url,@encodedHTML)
  end

  def index     
    @savedHTML = ScrapedPage.all
  end

  def show
    @savedHTML = ScrapedPage.find(params[:id])
  end

  def new
    @savedHTML = ScrapedPage.new
  end

  def create(url, scraped_html)
    saved_html = ScrapedPage.create(domain: url, html: scraped_html, css: '', javascript: '')

    if saved_html.save
      puts "ADDED TO THE DATABASE"
      redirect_to(root_path)
    else
      puts "FAILED TO ADD TO THE DATABASE"
    end
  end

  def edit
  end

  def upadate
  end

  def delete
    @savedHTML = ScrapedPage.find(params[:id])
  end

  def destroy
    @savedHTML = ScrapedPage.find(params[:id])
    @savedHTML.destroy
    redirect_to(root_path)
  end

  def compare
    coder = HTMLEntities.new

    @domain  = params[:domain].to_s
    puts "DOMAIN------------------------------------#{@domain}"
    puts "Param1------------------------------------#{params[:version_one]}"
    puts "Param2------------------------------------#{params[:version_two]}"
    puts "Param3------------------------------------#{params[:change_type]}"

    version_one = ScrapedPage.select("html").find_by(domain: @domain, created_at: params[:version_one]).html
    version_two = ScrapedPage.select("html").find_by(domain: @domain ,created_at: params[:version_two]).html

    test1 =  Nokogiri::HTML(coder.decode(version_one))
    test2 =  Nokogiri::HTML(coder.decode(version_two))

    if params[:change_type] == "business"
      compared_code = Diffy::Diff.new(test1.xpath("//div"), test2.xpath("//div")).to_s(:html).html_safe
      #concatenate multiple changes here for a change type
    elsif params[:change_type] == "developers"
      @script_changes = Diffy::Diff.new(test1.xpath("//script"), test2.xpath("//script")).to_s(:html).html_safe 
      @meta_changes = Diffy::Diff.new(test1.xpath("//meta"), test2.xpath("//meta")).to_s(:html).html_safe
      @changes = Diffy::Diff.new(test1.xpath("//meta"), test2.xpath("//meta")).to_s(:html).html_safe  
    end
  end

  def watched_link_params
    params.require(:default).permit(:url)
  end

  def compare_params
    params.require(:domain).permit(:domain)
  end

  def set_schedule
    puts @scrape_schedule = "#{params[:hour]}#{params[:date]}"
    schedule("in", @scrape_schedule, "cunt")
  end

  require 'rufus-scheduler'
  @job_id = nil

  @test = "NO!"
  SCHEDULER = Rufus::Scheduler.new

  def schedule(cmd, tim, msg)
    @job_running = true
    SCHEDULER.send(cmd, tim) do |job|
      now = Time.now
      now = "#{now.strftime('%Y-%m-%d %H:%M:%S')}.#{sprintf('%06d', now.usec)}"
      puts("#{now} : #{msg.inspect} : (#{job.id})")
      @job_id = job.id
    end 
  end

  def unschedule(_, job_id, _)
    if (@job_runing)
      SCHEDULER.unschedule(job_id)
      @job_id = nil
    end
  end
end