【问题标题】:How can I tell what apache rewrite rules are being applied to my requests?我如何知道哪些 apache 重写规则应用于我的请求?
【发布时间】:2014-12-02 02:13:39
【问题描述】:

我正在开发一个托管在共享服务器上的 Rails 应用程序。该应用程序位于子域:database.my_domain.org。我在设置时遇到了一些重写问题,在这篇文章中有详细说明:Extra directory prepended to my routes in production

现在我遇到了一个可能相关但最终不同的问题。每当用户尝试在应用程序中编辑或创建对象时,他们都会收到“您要查找的页面不存在”错误。我可以在日志中看到出了什么问题:应用程序向一个地址发起请求,但它作为对另一个地址的请求执行:

Started PUT "/individuals/4" for 72.93.46.141 at Tue Oct 07 10:54:37 -0400 2014

ActionController::RoutingError (No route matches [PUT] "/4"):
  actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
  activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.5) lib/rack/lock.rb:15:in `call'
  rack-cache (1.2) lib/rack/cache/context.rb:136:in `forward'
  rack-cache (1.2) lib/rack/cache/context.rb:143:in `pass'
  rack-cache (1.2) lib/rack/cache/context.rb:155:in `invalidate'
  rack-cache (1.2) lib/rack/cache/context.rb:71:in `call!'
  rack-cache (1.2) lib/rack/cache/context.rb:51:in `call'
  railties (3.2.13) lib/rails/engine.rb:479:in `call'
  railties (3.2.13) lib/rails/application.rb:223:in `call'
  railties (3.2.13) lib/rails/railtie/configurable.rb:30:in `send'
  railties (3.2.13) lib/rails/railtie/configurable.rb:30:in `method_missing'
  passenger (3.0.18) lib/phusion_passenger/rack/request_handler.rb:96:in `process_request'
  passenger (3.0.18) lib/phusion_passenger/abstract_request_handler.rb:516:in `accept_and_process_next_request'
  passenger (3.0.18) lib/phusion_passenger/abstract_request_handler.rb:274:in `main_loop'
  passenger (3.0.18) lib/phusion_passenger/rack/application_spawner.rb:206:in `start_request_handler'
  passenger (3.0.18) lib/phusion_passenger/rack/application_spawner.rb:171:in `send'
  passenger (3.0.18) lib/phusion_passenger/rack/application_spawner.rb:171:in `handle_spawn_application'
  passenger (3.0.18) lib/phusion_passenger/utils.rb:470:in `safe_fork'
  passenger (3.0.18) lib/phusion_passenger/rack/application_spawner.rb:166:in `handle_spawn_application'
  passenger (3.0.18) lib/phusion_passenger/abstract_server.rb:357:in `__send__'
  passenger (3.0.18) lib/phusion_passenger/abstract_server.rb:357:in `server_main_loop'
  passenger (3.0.18) lib/phusion_passenger/abstract_server.rb:206:in `start_synchronously'
  passenger (3.0.18) lib/phusion_passenger/abstract_server.rb:180:in `start'
  passenger (3.0.18) lib/phusion_passenger/rack/application_spawner.rb:129:in `start'
  passenger (3.0.18) lib/phusion_passenger/spawn_manager.rb:253:in `spawn_rack_application'
  passenger (3.0.18) lib/phusion_passenger/abstract_server_collection.rb:132:in `lookup_or_add'
  passenger (3.0.18) lib/phusion_passenger/spawn_manager.rb:246:in `spawn_rack_application'
  passenger (3.0.18) lib/phusion_passenger/abstract_server_collection.rb:82:in `synchronize'
  passenger (3.0.18) lib/phusion_passenger/abstract_server_collection.rb:79:in `synchronize'
  passenger (3.0.18) lib/phusion_passenger/spawn_manager.rb:244:in `spawn_rack_application'
  passenger (3.0.18) lib/phusion_passenger/spawn_manager.rb:137:in `spawn_application'
  passenger (3.0.18) lib/phusion_passenger/spawn_manager.rb:275:in `handle_spawn_application'
  passenger (3.0.18) lib/phusion_passenger/abstract_server.rb:357:in `__send__'
  passenger (3.0.18) lib/phusion_passenger/abstract_server.rb:357:in `server_main_loop'
  passenger (3.0.18) lib/phusion_passenger/abstract_server.rb:206:in `start_synchronously'
  passenger (3.0.18) helper-scripts/passenger-spawn-server:99

有一天,当没有人在应用程序上工作时,这出乎意料地开始了。我已将我的 rails 代码回滚到以前的版本,但这并没有解决问题,所以我相当确定 rails 代码不是问题。

我的子域 (~/public_html/database) 根目录中的 .htaccess 文件如下所示:

SetEnv GEM_HOME /home/user/ruby/gems
PassengerEnabled On
PassengerAppRoot /home/user/rails_apps/app_name/current/

RewriteEngine Off

我看不出这仍然会重写我的网址。

另一方面,~/public_html(上一个目录)中的 .htaccess 文件包含各种规则。我不认为他们正在被使用。我备份然后删除了文件,问题仍然存在。如果有帮助,这里是 ~/public_html/.htaccess 文件:

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
  RewriteEngine On

  # uncomment the following line, if you are having trouble
  # getting no_script_name to work
  #RewriteBase /

  # we skip all files with .something
  #RewriteCond %{REQUEST_URI} \..+$
  #RewriteCond %{REQUEST_URI} !\.html$
  #RewriteRule .* - [L]

#old links
redirect 301 /shop /chocolate_shop
redirect 301 /shop /chocolate_shop
redirect 301 /pg/show/id/4 /chocolate_shop

redirect 301 /pg/show/id/1 /our_philosophy
redirect 301 /pg/show/id/2 /our_story
redirect 301 /pg/show/id/3 /our_vision

redirect 301 /pg/show/id/4 /chocolate_shop
redirect 301 /pg/show/id/32 /chocolate_menu
redirect 301 /pg/show/id/5 /family
redirect 301 /pg/show/id/6 /garden
redirect 301 /pg/show/id/28 /garden_what_we_grow
redirect 301 /pg/show/id/23 /garden_a_typical_day
redirect 301 /pg/show/id/7 /festival
redirect 301 /pg/show/id/8 /catering
redirect 301 /pg/show/id/27 /catering_menu

#people pages
redirect 301 /pg/show/id/11/person/1 /people?johndoe
redirect 301 /pg/show/id/11/person/2 /people?janeplain
redirect 301 /pg/show/id/11/person/4 /people?joeshmoe
redirect 301 /pg/show/id/11/person/5 /people?joeshmoe
redirect 301 /pg/show/id/11/person/8 /people?joeshmoe
redirect 301 /pg/show/id/11/person/9 /people?joeshmoe
redirect 301 /pg/show/id/11/person/11 /people?joeshmoe
redirect 301 /pg/show/id/11/person/19 /people?joeshmoe
redirect 301 /pg/show/id/11/person/20 /people?joeshmoe
redirect 301 /pg/show/id/11/person/21 /people?joeshmoe
redirect 301 /pg/show/id/11/person/22 /people?joeshmoe
redirect 301 /pg/show/id/11/person/23 /people?joeshmoe
redirect 301 /pg/show/id/11/person/24 /people?joeshmoe
redirect 301 /pg/show/id/11/person/26 /people?joeshmoe
redirect 301 /pg/show/id/11/person/27 /people?joeshmoe
redirect 301 /pg/show/id/11/person/28 /people?joeshmoe
redirect 301 /pg/show/id/11/person/29 /people?joeshmoe
redirect 301 /pg/show/id/11/person/30 /people?joeshmoe
redirect 301 /pg/show/id/11/person/31 /people?joeshmoe
redirect 301 /pg/show/id/11/person/34 /people?joeshmoe
redirect 301 /pg/show/id/11/person/42 /people?joeshmoe
redirect 301 /pg/show/id/11/person/43 /people?joeshmoe
redirect 301 /pg/show/id/11/person/44 /people?joeshmoe
redirect 301 /pg/show/id/11/person/45 /people?joeshmoe
redirect 301 /pg/show/id/11/person/46 /people?joeshmoe
redirect 301 /pg/show/id/11/person/47 /people?joeshmoe
redirect 301 /pg/show/id/11/person/48 /people?joeshmoe
redirect 301 /pg/show/id/11/person/50 /people?joeshmoe
redirect 301 /pg/show/id/11/person/51 /people?joeshmoe
redirect 301 /pg/show/id/11/person/52 /people?joeshmoe

redirect 301 /pg/show/id/11 /people
redirect 301 /pg/show/id/12 /learning
redirect 301 /pg/show/id/13 /events

#updates pages
redirect 301 /pg/show/id/36 /feed
redirect 301 /pg/show/id/17 /news

#give pages
redirect 301 pg/show/id/14 /give
redirect 301 pg/show/id/15 /volunteer
redirect 301 pg/show/id/16 /goods
redirect 301 pg/show/id/30 /volunteer_form

#policy page
redirect 301 /pg/show/id/34 /privacy

#search page
redirect 301 /pg/show/id/35 /search

# Internally rewrite URLs requested with no filetype to php scripts
# unless the requested URL resolves to an existing directory or file.
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /$1.shtml [L]

  # we check if the .html version is here (caching)
  #RewriteRule ^$ index.html [QSA]
  #RewriteRule ^([^.]+)$ $1.html [QSA]
  #RewriteCond %{REQUEST_FILENAME} !-f

  # no, so we redirect to our front web controller
  #RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

RewriteCond %{HTTP_HOST} ^phillycatering\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.phillycatering\.com$
RewriteRule ^/?$ "http\:\/\/my_domain\.org\/chocolate_shop" [R=301,L]

重写规则是否在子目录中继承?有没有办法告诉我的请求应用了哪些 apache 重写规则?

【问题讨论】:

    标签: ruby-on-rails apache .htaccess


    【解决方案1】:

    除非您在子目录中有不同的规则,否则它们会被继承到子目录。默认情况下,重写引擎是关闭的,所以关闭它不会改变任何东西。相反,您需要将其打开,但没有任何规则。

    SetEnv GEM_HOME /home/user/ruby/gems
    PassengerEnabled On
    PassengerAppRoot /home/user/rails_apps/app_name/current/
    
    RewriteEngine On
    

    这告诉重写引擎您希望重写引擎启动并应用您在此目录上下文中拥有的任何规则(没有),这实际上取代了您在父目录中拥有的任何规则。

    【讨论】:

    • 谢谢乔恩!这很有意义,但不幸的是它并没有改变任何东西。还有其他想法吗?
    • @lucas 我唯一能想到的另一件事是将RewriteRule ^database/ - [L] 添加到父 htaccess 文件的最顶部。
    • 这是个好主意。它仍然没有改变任何东西,但我认为至少这消除了规则可能从父 htaccess 文件继承的可能性。
    猜你喜欢
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多