My mod_rewrite rules do not resolve PHP variables

2019-08-12 19:35发布

问题:

I have an interesting situation. I've configured a basic LAMP server and an external company is providing us some code. Everything works except PHP rewrites specifically.

I've successfully configured rewrites and can do basic rewrites like hello.html -> redir.html. I've also confirmed this by turning LogLevel up to trace6 and I see rewrite being active.

the trouble comes with php variables. It seems to just not resolve them.

I have the following simplified .htaccess file with all other rules removed for testing

<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^news/(.*)/(.*)/(.*)/$ news.php?department=$1&newsid=$2&newstitle=$3 [L]
RewriteRule ^$ index.php?page=home [L]

</ifModule>

now if I enter my URL http://webserver/news/it/153/newsstory

I expect to be redirected to

http://webserver/news.php?department=it&newsid=153&newstitle=newsstory

however, what happens is the second rule catches it.

this is tricky for me, as I'm providing the server infrastructure and not writing the code (externally supplied). Our code supplier says it works their side and we've seen working demos, so I've been sort of troubleshooting it from a 'we are missing packages or configs somehow' angle.

As far as I can tell online, if mod_rewrite is working, it should... work.

a log dump of an incorrect redir

[perdir /var/www/itnews/] add path info postfix: /var/www/itnews/news.php -> /var/www/itnews/news.php/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/news.php/it/153/newsstory -> news.php/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^news/(.*)/(.*)/(.*)$' to uri 'news.php/it/153/newsstory'
[perdir /var/www/itnews/] add path info postfix: /var/www/itnews/news.php -> /var/www/itnews/news.php/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/news.php/it/153/newsstory -> news.php/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^$' to uri 'news.php/it/153/newsstory'
[perdir /var/www/itnews/] pass through /var/www/itnews/news.php
[perdir /var/www/itnews/] add path info postfix: /var/www/itnews/news.php -> /var/www/itnews/news.php/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/news.php/it/153/newsstory -> news.php/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^news/(.*)/(.*)/(.*)$' to uri 'news.php/it/153/newsstory'
[perdir /var/www/itnews/] add path info postfix: /var/www/itnews/news.php -> /var/www/itnews/news.php/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/news.php/it/153/newsstory -> news.php/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^$' to uri 'news.php/it/153/newsstory'
[perdir /var/www/itnews/] pass through /var/www/itnews/news.php
[perdir /var/www/itnews/] add path info postfix: /var/www/itnews/it -> /var/www/itnews/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/it/153/newsstory -> it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^news/(.*)/(.*)/(.*)$' to uri 'it/153/newsstory'
[perdir /var/www/itnews/] add path info postfix: /var/www/itnews/it -> /var/www/itnews/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/it/153/newsstory -> it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^$' to uri 'it/153/newsstory'
[perdir /var/www/itnews/] pass through /var/www/itnews/it
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/images/logo.jpg -> images/logo.jpg, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^news/(.*)/(.*)/(.*)$' to uri 'images/logo.jpg', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/images/logo.jpg -> images/logo.jpg, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^$' to uri 'images/logo.jpg', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] pass through /var/www/itnews/images/logo.jpg, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/css/bootstrap.min.css -> css/bootstrap.min.css, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^news/(.*)/(.*)/(.*)$' to uri 'css/bootstrap.min.css', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/css/bootstrap.min.css -> css/bootstrap.min.css, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^$' to uri 'css/bootstrap.min.css', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] pass through /var/www/itnews/css/bootstrap.min.css, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/css/style.css -> css/style.css, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^news/(.*)/(.*)/(.*)$' to uri 'css/style.css', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/css/style.css -> css/style.css, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^$' to uri 'css/style.css', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] pass through /var/www/itnews/css/style.css, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/js/bootstrap.min.js -> js/bootstrap.min.js, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^news/(.*)/(.*)/(.*)$' to uri 'js/bootstrap.min.js', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/js/bootstrap.min.js -> js/bootstrap.min.js, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^$' to uri 'js/bootstrap.min.js', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] pass through /var/www/itnews/js/bootstrap.min.js, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/css/prettyCheckable.css -> css/prettyCheckable.css, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^news/(.*)/(.*)/(.*)$' to uri 'css/prettyCheckable.css', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/css/prettyCheckable.css -> css/prettyCheckable.css, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^$' to uri 'css/prettyCheckable.css', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] pass through /var/www/itnews/css/prettyCheckable.css, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/js/jquery-1.11.0.min.js -> js/jquery-1.11.0.min.js, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^news/(.*)/(.*)/(.*)$' to uri 'js/jquery-1.11.0.min.js', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/js/jquery-1.11.0.min.js -> js/jquery-1.11.0.min.js, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^$' to uri 'js/jquery-1.11.0.min.js', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] pass through /var/www/itnews/js/jquery-1.11.0.min.js, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/plugins/timthumb.php -> plugins/timthumb.php, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^news/(.*)/(.*)/(.*)$' to uri 'plugins/timthumb.php', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/plugins/timthumb.php -> plugins/timthumb.php, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^$' to uri 'plugins/timthumb.php', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] pass through /var/www/itnews/plugins/timthumb.php, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/plugins/timthumb.php -> plugins/timthumb.php, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^news/(.*)/(.*)/(.*)$' to uri 'plugins/timthumb.php', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/plugins/timthumb.php -> plugins/timthumb.php, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^$' to uri 'plugins/timthumb.php', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] pass through /var/www/itnews/plugins/timthumb.php, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/plugins/timthumb.php -> plugins/timthumb.php, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^news/(.*)/(.*)/(.*)$' to uri 'plugins/timthumb.php', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/plugins/timthumb.php -> plugins/timthumb.php, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^$' to uri 'plugins/timthumb.php', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] pass through /var/www/itnews/plugins/timthumb.php, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/plugins/timthumb.php -> plugins/timthumb.php, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^news/(.*)/(.*)/(.*)$' to uri 'plugins/timthumb.php', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/plugins/timthumb.php -> plugins/timthumb.php, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^$' to uri 'plugins/timthumb.php', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] pass through /var/www/itnews/plugins/timthumb.php, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/plugins/timthumb.php -> plugins/timthumb.php, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^news/(.*)/(.*)/(.*)$' to uri 'plugins/timthumb.php', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/plugins/timthumb.php -> plugins/timthumb.php, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^$' to uri 'plugins/timthumb.php', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] pass through /var/www/itnews/plugins/timthumb.php, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/fonts/ocean_sans_std_book-webfont.woff -> fonts/ocean_sans_std_book-webfont.woff, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^news/(.*)/(.*)/(.*)$' to uri 'fonts/ocean_sans_std_book-webfont.woff', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/fonts/ocean_sans_std_book-webfont.woff -> fonts/ocean_sans_std_book-webfont.woff, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] applying pattern '^$' to uri 'fonts/ocean_sans_std_book-webfont.woff', referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] pass through /var/www/itnews/fonts/ocean_sans_std_book-webfont.woff, referer: http://webserver/news/it/153/newsstory
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/images/body-bg.jpg -> images/body-bg.jpg, referer: http://webserver/css/style.css?v=1.0.1
[perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/images/body-bg.jpg -> images/body-bg.jpg, referer: http://webserver/css/style.css?v=1.0.1
[perdir /var/www/itnews/] applying pattern '^$' to uri 'images/body-bg.jpg', referer: http://webserver/css/style.css?v=1.0.1
[perdir /var/www/itnews/] pass through /var/www/itnews/images/body-bg.jpg, referer: http://webserver/css/style.css?v=1.0.1

RELATED: I've just gone through all the php pages I can find and turned on error logging to maximum.

I'm getting

Notice: Undefined index: department in /var/www/itnews/news.php on line 15

Notice: Undefined index: newsid in /var/www/itnews/news.php on line 18

Notice: Undefined index: archive in /var/www/itnews/pages/news-archive.php on line 16

Notice: Undefined variable: page in /var/www/itnews/pages/news-archive.php on line 28

on the main page. I'm now thinking that something in php is faulting out and the redirects are actually failing because php itself is failing to resolve its variables.

is this likely?

回答1:

this url http://webserver/news/it/153/newsstory cannot match your first rule because of the / at the end.

# do not match
RewriteRule ^news/(.*)/(.*)/(.*)/$ news.php?department=$1&newsid=$2&newstitle=$3 [L]

# will match
RewriteRule ^news/(.*)/(.*)/(.*)$ news.php?department=$1&newsid=$2&newstitle=$3 [L]

You can use this tool to debug your .htaccess files.