Paul R. Ganci
2016-May-28 19:03 UTC
[CentOS] CentOS 6.8 Apache-2.2.15-53 re-write question
I was wondering if somebody could help me with an Apache re-write rule. Apparently CentOS 6.8 is running apache-2.2.15-53. I am trying to redirect all pages except for two pages. The apache rewrite directives in the httpd config are: RewriteEngine on RewriteCond %{REQUEST_URI}!^/test/ RewriteCond %{REQUEST_URI}!^/my-folder/ RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L] These commands should cause all requests to get re-directed to www.newdomain.com except for those with a /test or /my-folder on the end of the URL. However when I place these commands into the the httpd config apache spits out this error message: Starting httpd: Syntax error on line 31 of /etc/httpd/conf.d/olddomain.conf: RewriteCond: bad argument line '%{REQUEST_URI}!^/test/ I have of course renamed the real config parameters. Can somebody tell me if these directives only work in apache-2.4 and if so how do I do the same thing in apache-2.2? If these directives should work in apache-2.2 is this an apache problem? Thanks for your help. -- Paul (ganci at nurdog.com) Cell: (303)257-5208
On 28.05.2016 21:03, Paul R. Ganci wrote:> I was wondering if somebody could help me with an Apache re-write > rule. Apparently CentOS 6.8 is running apache-2.2.15-53. I am trying > to redirect all pages except for two pages. The apache rewrite > directives in the httpd config are: > > RewriteEngine on > RewriteCond %{REQUEST_URI}!^/test/ > RewriteCond %{REQUEST_URI}!^/my-folder/ > RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L] > > These commands should cause all requests to get re-directed to > www.newdomain.com except for those with a /test or /my-folder on the > end of the > URL. However when I place these commands into the the httpd config > apache spits out this error message: >^ is the beginning marker of a regular expression; are you sure you want at the end? RewriteEngine on RewriteCond %{REQUEST_URI} !^/test/ RewriteCond %{REQUEST_URI} !^/my-folder/ RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301]> Starting httpd: Syntax error on line 31 of > /etc/httpd/conf.d/olddomain.conf: > RewriteCond: bad argument line '%{REQUEST_URI}!^/test/caused by missing whitespace> I have of course renamed the real config parameters. Can somebody tell > me if these directives only work in apache-2.4 and if so how do I do the > same thing in apache-2.2? If these directives should work in > apache-2.2 is this an apache problem?these should work in 2.2.x, too;
Alexander Dalloz
2016-May-28 19:35 UTC
[CentOS] CentOS 6.8 Apache-2.2.15-53 re-write question
Am 28.05.2016 um 21:03 schrieb Paul R. Ganci:> I was wondering if somebody could help me with an Apache re-write rule. > Apparently CentOS 6.8 is running apache-2.2.15-53. I am trying > to redirect all pages except for two pages. The apache rewrite > directives in the httpd config are: > > RewriteEngine on > RewriteCond %{REQUEST_URI}!^/test/ > RewriteCond %{REQUEST_URI}!^/my-folder/ > RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L] > > These commands should cause all requests to get re-directed to > www.newdomain.com except for those with a /test or /my-folder on the end > of the > URL. However when I place these commands into the the httpd config > apache spits out this error message: > > Starting httpd: Syntax error on line 31 of > /etc/httpd/conf.d/olddomain.conf: > RewriteCond: bad argument line '%{REQUEST_URI}!^/test/You missed a whitespace between the server variable %{REUQEST_URI} and the value you test against. In both cases you did. And you probly want a trailing [OR] parameter. See https://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond> I have of course renamed the real config parameters. Can somebody tell > me if these directives only work in apache-2.4 and if so how do I do the > same thing in apache-2.2? If these directives should work in apache-2.2 > is this an apache problem?Works with Apache 2.2, see cited docs above.> Thanks for your help.Alexander
Paul R. Ganci
2016-May-28 20:08 UTC
[CentOS] CentOS 6.8 Apache-2.2.15-53 re-write question
On 05/28/2016 01:35 PM, Alexander Dalloz wrote:> Am 28.05.2016 um 21:03 schrieb Paul R. Ganci: >> Starting httpd: Syntax error on line 31 of >> /etc/httpd/conf.d/olddomain.conf: >> RewriteCond: bad argument line '%{REQUEST_URI}!^/test/ > > You missed a whitespace between the server variable %{REUQEST_URI} and > the value you test against. In both cases you did. And you probly want > a trailing [OR] parameter. See > > https://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecondI did the suggested changes and indeed the syntax error goes away but the code does not work. The redirection takes place for all the pages even for those specified in the RewriteCond lines. My immediate syntax error problem is solved. It is funny how I stared at these lines for what seemed like eons and my mind fixed the immediate problem. Thanks for your help, Alexander. I will take another look at what I did and see if I can't find what else is wrong. -- Paul (ganci at nurdog.com) Cell: (303)257-5208
Always Learning
2016-May-29 16:33 UTC
[CentOS] CentOS 6.8 Apache-2.2.15-53 re-write question
On Sat, 2016-05-28 at 13:03 -0600, Paul R. Ganci wrote: .......... How about RewriteEngine on RewriteCond %{REQUEST_URI} !^/test/ [NC] RewriteCond %{REQUEST_URI} !^/my-folder/ [NC] RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L] or RedirectMatch 301 ^/test/(.*)$ /test2/$1 RedirectMatch 301 ^/my-folder/(.*)$ /my-folder2/$1 RedirectMatch 301 ^(.*)$ http://new.domain.com/$1 or RedirectMatch 301 ^/usual-directories-names-prefix/(.*)$ http://new.domain.com/new-directory-names-prefix$1 For many years the practise of using the 'www' prefix has been depreciated by using just the domain name but also having a DNS A record for the www for those that love the 'www' prefix. Having a directory names prefix helps enormously when maintaining multiple web sites. Many web sites have a confused illogical jumble of directory names, and many of those are far too long. Good planing before going live will subsequently save lots of time and effort. Keep the public directory structure plain, simple and logical. One never ever needs URL's exceeding 80 characters. Avoid things like this http://www/domain.com/public-communications/press-and-public/press-releases/press-release-12345-man-hits-dog-in-childrens-park.html and http://www.dailymail.co.uk/news/article-3615099/Outrage-directed-parents-Harambe-gorilla-s-senseless-death-four-year-old-son-fell-enclosure-led-zoo-officials-fatally-shoot-animal.html If anyone has to type-in one of your URLs they will definitely appreciate short URLs where there is less chance of making spelling mistakes. -- Regards, Paul. England, EU. England's place is in the European Union.
Always Learning
2016-May-29 16:36 UTC
[CentOS] CentOS 6.8 Apache-2.2.15-53 re-write question
On Sun, 2016-05-29 at 17:33 +0100, Always Learning wrote:> RedirectMatch 301 ^(.*)$ http://new.domain.com/$1should be:- RedirectMatch 301 ^/(.*)$ http://new.domain.com/$1 -- Regards, Paul. England, EU. England's place is in the European Union.