[SOLVED] Need correct syntax for httpd rewrite or redirect
Linux - DesktopThis forum is for the discussion of all Linux Software used in a desktop context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
... 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?
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:
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:
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:
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.
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.
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:
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)
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.