LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 02-04-2019, 01:07 AM   #1
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,561

Rep: Reputation: 177Reputation: 177
Need correct syntax for httpd rewrite or redirect


Given a URL: http://host.mydom.com, I want to rewrite that with args: http://host.mydom.com/?arg1=joe. In httpd.conf I've tried:
Code:
RewriteEngine on
RewriteRule "^http://host.mydom.com$" "http://host.mydom.com/?arg1=joe" [R]

; AND

RewriteEngine On
RewriteCond %{HTTP_HOST}  ^http://host.mydom.com$
RewriteRule  ^(.*)$  http://host.mydom.com/?arg1=joe [R]
Neither work. Guidance appreciated.
 
Old 02-04-2019, 06:14 AM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
Originally Posted by mfoley View Post
Given a URL: http://host.mydom.com, I want to rewrite that with args: http://host.mydom.com/?arg1=joe. In httpd.conf I've tried:
Code:
RewriteEngine on
RewriteRule "^http://host.mydom.com$" "http://host.mydom.com/?arg1=joe" [R]

; AND

RewriteEngine On
RewriteCond %{HTTP_HOST}  ^http://host.mydom.com$
RewriteRule  ^(.*)$  http://host.mydom.com/?arg1=joe [R]
Neither work. Guidance appreciated.
The 1st rewrite is wrong. You cannot have a URL in the left side or the RewriteRule.
The 2nd probably gives a loop, because you rewrite everything to an external URL (the same host actually) with no conditions.

You may try this:
Code:
RewriteEngine on

RewriteCond %{QUERY_STRING} !arg1
RewriteRule  ^(.*)$  http://host.mydom.com/?arg1=joe [R]

Last edited by bathory; 02-04-2019 at 06:35 AM. Reason: typos
 
Old 02-04-2019, 06:45 AM   #3
dc.901
Senior Member
 
Registered: Aug 2018
Location: Atlanta, GA - USA
Distribution: CentOS/RHEL, openSuSE/SLES, Ubuntu
Posts: 1,005

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Many years ago, I had a redirect within a HTML page, like this:

https://www.w3docs.com/snippets/html...e-in-html.html

This was in early version of Apache 2.x
 
Old 02-04-2019, 11:44 AM   #4
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,561

Original Poster
Rep: Reputation: 177Reputation: 177
Quote:
Originally Posted by bathory View Post
... The 2nd probably gives a loop, because you rewrite everything to an external URL (the same host actually) with no conditions.
Hmmm, I do have ^ and $ on either side of the URL in the RewriteCond, so I would have expected it to not loop since the resulting redirect would not match exactly. In any case, it didn't work.
Quote:
You may try this:
Code:
RewriteEngine on

RewriteCond %{QUERY_STRING} !arg1
RewriteRule  ^(.*)$  http://host.mydom.com/?arg1=joe [R]
[/quote]
That worked, sort of. It did bring up the page, but all images references by the page did not show. Perhaps the image references also has 'arg1' added to their URLs.

The other issue is that not all pages on this site will get the arg1=joe. With the rule you've shown, what's to prevent ALL URLs on this site from getting the arg1=joe added?

Later ...

When I tried later, the image did show! Puzzling.
 
Old 02-04-2019, 11:26 PM   #5
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,561

Original Poster
Rep: Reputation: 177Reputation: 177
In fact, the Rule does change all pages on this site to http://host.mydom.com/?arg1=joe. For example, http://yourdom.org (also hosted on this site) gets transformed. So, that's not going to work. How about:
Code:
RewriteCond %{QUERY_STRING} http://host.mydom.com/
RewriteCond %{QUERY_STRING} !arg1

or

RewriteCond %{HTTP_HOST}  http://host.mydom.com
RewriteCond %{QUERY_STRING} !arg1
I'll do some experimenting, but expert suggestions are appreciated. I'm no rewrite rule guru.

Last edited by mfoley; 02-04-2019 at 11:30 PM.
 
Old 02-05-2019, 04:14 AM   #6
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
Originally Posted by mfoley View Post
In fact, the Rule does change all pages on this site to http://host.mydom.com/?arg1=joe. For example, http://yourdom.org (also hosted on this site) gets transformed. So, that's not going to work. How about:
Code:
RewriteCond %{QUERY_STRING} http://host.mydom.com/
RewriteCond %{QUERY_STRING} !arg1

or

RewriteCond %{HTTP_HOST}  http://host.mydom.com
RewriteCond %{QUERY_STRING} !arg1
I'll do some experimenting, but expert suggestions are appreciated. I'm no rewrite rule guru.
You should do some tests by yourself, before asking...
Anyway, they're both not going to work. The HTTP_HOST variable is just a hostname, not the full URL. So you can use:
Code:
RewriteCond %{HTTP_HOST}  host.mydom.com [NC]
RewriteCond %{QUERY_STRING} !arg1
Note also, that you can avoid this, if you stick the Rewrite stuff into the appropriate vhost definition stanza, or in a .htaccess in that vhost docroot.

FRegards
 
Old 02-05-2019, 12:26 PM   #7
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,561

Original Poster
Rep: Reputation: 177Reputation: 177
Quote:
Originally Posted by bathory View Post
You should do some tests by yourself, before asking...
I have been and continue to experiment. I come here when making no headway. I find rewrite rules maddening.
Quote:
Anyway, they're both not going to work. The HTTP_HOST variable is just a hostname, not the full URL. So you can use:
Code:
RewriteCond %{HTTP_HOST}  host.mydom.com [NC]
RewriteCond %{QUERY_STRING} !arg1
I've tried the following:
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST}        host.mydom.com
RewriteCond %{QUERY_STRING}     !arg1=
RewriteRule     ^(.*)$          http://host.mydom.com/?arg1=joe [R]
Note the your [NC] flag suggestion doesn't apply to RewriteCond, only RewriteRule.

The above is an improvement. Now, other domains hosted on this site do not all revert to host.mydom.com/?arg1=joe. However, The images in the html: <img src="image/mypix.jpg"> no longer show up. The access_log gives a 302 Found redirect status for images. I've tried adding "RewriteCcond %{QUERY_STRING} !images", and I've tried putting ^ and $ on either side of the first RewriteCond. These don't work. Not sure how to fix this. The access_log message is:
Code:
"GET /images/menu.jpg HTTP/1.1" 302 235
Sheesh! Even what seems to be a rather simple rewrite task turns out to be a multi-hour project!
Quote:
Note also, that you can avoid this, if you stick the Rewrite stuff into the appropriate vhost definition stanza, or in a .htaccess in that vhost docroot.

FRegards
At the moment, I'm not using vhosts. The page is ultimately served by tomcat so I have a <host> section in server.xml specifying the webapp folder. This rewrite will occur before tomcat gets a hold of it. Putting an .htaccess file in the directory won't any better because multiple URLs will get redirected there.

If there doesn't seem to be an obvious solution here, I'll consider rewriting the application to avoid using the arg1= parameter.
 
Old 02-05-2019, 03:39 PM   #8
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
Note the your [NC] flag suggestion doesn't apply to RewriteCond, only RewriteRule.
Wrong! [NC] goes to RewriteCond...


Quote:
I've tried adding "RewriteCcond %{QUERY_STRING} !images", and I've tried putting ^ and $ on either side of the first RewriteCond. These don't work. Not sure how to fix this
%{QUERY_STRING} is what follows a "?" in a URL, so there is no "RewriteCcond %{QUERY_STRING} !images" here.
You could use the following (ugly) way:
Code:
%{REQUEST_URI} !(.*).png [NC,OR]
%{REQUEST_URI} !(.*).jpg [NC,OR]
%{REQUEST_URI} !(.*).gif [NC,OR]
and so on...


Quote:
The page is ultimately served by tomcat so I have a <host> section in server.xml specifying the webapp folder. This rewrite will occur before tomcat gets a hold of it. Putting an .htaccess file in the directory won't any better because multiple URLs will get redirected there.
If that's the case better use mod_jk, where you can specify what can be served by apache and what will be passed to the backend tomcat (Hint: JkMount vs JkUnMount)
 
Old 02-06-2019, 02:25 PM   #9
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,561

Original Poster
Rep: Reputation: 177Reputation: 177
Thanks for your perseverance in this question, but I've decided this particular rewrite rule is above my skill level and, as I have several hosts to implement, this could get extremely complicated. I've decided to take another approach. The 'hosts' in host.mydom.com have a one-to-one relationship with the value of arg1. Therefore, what I'll do is examine the URL and parse of the host bit. That will get me what I need without rewrite rules.
 
  


Reply

Tags
httpd, rewriterule



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Correct method to setup Slim framework and setting url rewrite newbie14 Programming 2 02-23-2018 09:58 PM
httpd: Syntax error on line 210 of /etc/httpd/conf/httpd.conf: Syntax error on line 6 iswarya Linux - Newbie 1 01-25-2012 01:28 PM
mod_rewrite: how to redirect but _not_ rewrite urentity Linux - Server 2 02-03-2009 06:02 AM
Starting httpd: httpd: Syntax error on line 209 of /etc/httpd/conf/httpd.conf: Syntax sethukpathi Linux - Networking 6 04-12-2008 11:26 AM
Failed to start apache :Starting httpd: Syntax error on line 1027 of /etc/httpd/conf/ payjoe Linux - Newbie 3 09-21-2007 07:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 10:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration