Sorry, my initial description was to short.
What I need to do is, to do two redirects on one base-url with two different conditions.
First is to redirect a request with just the base-url (like _http://some_url.org) to _http://some_other_url.org .
_http://some_url.org -> _http://some_other_url.org
The second one is, if _http://some_url.org/some_subdir is requested it shoud be redirected to SSL of the same host _https://some_url.org/some_subdir .
So in case 2 it should not be redirected to some_other_url.org .
_http://some_url.org/some_subdir -> https_://some_url.org/some_subdir
Both rewrites are not verry complex for its own, but I can't get them working together.
So my problem is, how do I have to set the RewriteCond (to be exact the regexp) to do the matching. The http->https overrules always.
I did several experiments with RewriteCond, RewriteRule, RedirectMatch but I didn't get it. Does anybody have an idea?
Oh, btw this should be done by the vhost-config, not with .htaccess-files in document-folders. It's a restriction I'm not responsible for :-( cause developers sometimes empty the doc-folders.
Sorry, I had to do this _http because of this editor to not recognize this "URLs".
thanks a lot
best regards, M.
I got the answer, thanks to Jim from google-groups
RewriteRule ^/$ http://some_other_url.org [R=permanent,L,NS]
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^/(.*) https://some_url.org/$1 [R=permanent,L,NS]
PERFECT! Thanks a lot Jim.
This exactly solves my Problem.
Best regards, M.
Related
I have checked various topics and nothing caught my eyes. This is what am trying to do ..
It's a small site and with only few pages all in my root /mobile folder. So I decided to modify h*p://example.com/mobile/academics.php to h*p://example.com/mobile/academics (without the trailing slash)
RewriteRule ^([^.]+)$ $1.php [NC,L] - Works fine.
But I have http://example.com/mobile/program.php?p=ams which I want to convert as http://example.com/mobile/program/ams . I tried this :
RewriteRule ^/program/([^/]+)$ program.php?p=$1 - Makes no effect. Browser keeps looking for /program/ams.php
How to have both rules coexist? I have query string only on program.php . Any help is appreciated. I am sorry if this has been answered before. I searched for quite sometime and couldn't find any.
Thanks,
Vik
use
RewriteRule ^/?program/([^/]+)$ program.php?p=$1
I hope someone can answer "why" this is the case:
There are times I can use:
...
RewriteRule ^(.*)$ index.php/$1 [L]
and then there are times where the above doesn't work and I must use:
...
RewriteRule ^(.*)$ index.php?/$1 [L]
the main difference being the addition of ? ... I typically see this happen on different system setups, fastcgi vs module vs cgi, but haven't done enough setups to see the pattern.
I am guessing that it is related to how the apache/setup parses path/path_info data. Any thoughts are welcomed, ideally I'd like to have a solid explanation of why this is and when it occurs.
On the same thread ... Sometimes Apache does not output PATH_INFO environment var which might be the root cause of this, but I wonder why that is.
The ? is the marker of the beginning of the query string.
So basically your first rule rewrite a URL "x" to a file "x" in the directory index.php, the second rewrite a URL "x" to the index.php file with parameter "x". [(BTW I don't know how to retrieve a variable with no name in the file, usually you use ?var=value&var2=value2 etc...)
OK I have been messing about with URL rewriting for the last few days and seem to have come to a bit of a dead-end. I have come up with a few solutions that work on some servers and not others, and my hosting company (1and1 - be vary wary of these guys if you choose them as hosts) hasn't been able to help at all.
My problem is this, i want to re-write this URL:
/result.php?section=[section name]&url=[url]
to this (adding a trailing slash if there is none):
/article/[section name]/[url]/
and ALSO
section.php?section=[section name]
to (again adding a trailing slash):
/section/[section name]/
each attempt seems to have different results. currently I am using thhe following, which works locally, but on the live server only the 'articles' rewrite works:
RewriteRule ^article/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ /article/$1/$2/
RewriteRule ^article/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ /result.php?section=$1&url=$2
RewriteRule ^section/([a-zA-Z0-9_-]+)$ /section/$1/
RewriteRule ^section/([a-zA-Z0-9_-]+)/$ section.php?section=$1
Can anyone help me come up with a solution that will work nicely? Thank you in advance, I am really struggling with what seems like something relatively straightforward...
Firstly in order to force the backslash on the end you will need to actually do a redirect...
RewriteRule ^article/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ /article/$1/$2/ [R=302,L]
RewriteRule ^section/([a-zA-Z0-9_-]+)$ /section/$1/ [R=302,L]
Note: I use 302 because 301 can be a pain during testing, once everything is working and you are happy with it change it to 301.
The two remaining rules are the rewrites that simply mask/alias the query_string URL, which should look something like this...
RewriteRule ^article/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ /result.php?section=$1&url=$2
RewriteRule ^section/([a-zA-Z0-9_-]+)/$ /section.php?section=$1
I'd like to rewrite:
www.example.com/file.html?username=john&number=1234
To:
www.example.com/users/john
But I can't figure out how to extract the "username" value from the query string. I've been Googling this all morning and reading the official docs but no luck. I need to solve this problem with a rewrite, rather than changing the application.
Any help much appreciated!
Rangi
RewriteCond %{QUERY_STRING} username=([^&]+)
RewriteRule /?file.html /users/%1
Going to http://example.com/file.html?username=foobar will then redirect you to http://example.com/users/foobar, add an [R] to the end if you need an external redirect.
Mostly the rewrites are done the other way around, it's rare to see someone who wants a querystring in 'outside' urls but doesn't have them internally. Or did I understand your question backwards?
Ok I've solved this using two rules, although not sure if I'm doing it the best way.
RewriteRule ^file.html xxx/%{QUERY_STRING} [L]
RewriteRule ^xxx/[^=]*=([^&]*) /users$1 [R=301,L]
The first rule makes the query string part of the URL, so the second rule can see it, and therefore match and rewrite parts of it. I used "xxx" but it could be anything.
The web server is Apache. I want to rewrite URL so a user won't know the actual directory. For example:
The original URL:
www.mydomainname.com/en/piecework/piecework.php?piecework_id=11
Expected URL:
piecework.mydomainname.com/en/11
I added the following statements in .htaccess:
RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.mydomainname\.com$ [NC]
RewriteRule ^(w+)/(\d+)$ /$1/%1/%1.php?%1_id=$2 [L]
Of course I replaced mydomainname with my domain name.
.htaccess is placed in the site root, but when I access piecework.mydomainname.com/en/11, I got "Object not found".(Of course I replaced mydomainname with my domain name.)
I added the following statements in .htaccess:
RewriteRule ^/(.*)/en/piecework/(.*)piecework_id=([0-9]+)(.*) piecework.mydomainname.com/en/$3
Of course I replaced mydomainname with my domain name.
.htaccess is placed in the site root, but when I access piecework.mydomainname.com/en/11, I got "Object not found".(Of course I replaced mydomainname with my domain name.)
What's wrong?
Try using RewriteLog in your vhost or server-conf in order to check the rewriting process. Right now you just seem to guess what mod_rewrite does.
By using RewriteLogLevel you can modify the extend of the logging. For starters I'd recommend level 5.
PS: Don't forget to reload/restart the server after modifying the config.
Here's a quick overview of what's happening:
RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.mydomainname\.com$ [NC]
First, the question mark is supposed to be at the end.
$1 would (should) match anything that is not 'www' 0 or 1 times.
$2 matches anything that is not a character 1 or more times, which theoretically would match a blank space there but likely would never match anything.
Then it requires '.mydomainname.com' after those two groupings.
Your first two conditions are looking for two separate groupings.
I'm not sure exactly how you're trying to set up your structure, but here is how I would write it based on your original and expected URL's:
RewriteCond %{HTTP_HOST} !^www\.mydomainname\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(\w+)\.mydomainname\.com$ [NC]
RewriteRule ^(\w+)/(\d+)$ /$1/%1/%1.php?%1_id=$2 [L]
Basically, your first condition is to make sure it's not the URL beginning with 'www' (it's easier to just make it a separate rule). The second condition makes it check any word that could possibly be in front of your domain name. Then your rewrite rule will redirect it appropriately.
Get rid of the last .htaccess line there in your question...
Someone please correct me if I typed something wrong. I don't remember if you have to have the '\' in front of '\w' and '\d' but I included them anyways.
You are doing it backwards. The idea is that you will give people the friendly address, and the re-write rule will point requests to this friendly, non-existent page to the real page without them seeing it. So right now you have it only handling what to do when they go to the ugly URL, but you are putting in the friendly URL. since no rule exists for when people put the friendly URL directly, apache looks for it and says "Object not Found"
So add a line:
RewriteRule piecework.mydomainname.com/en/(*.) ^/$3/en/piecework/$3?piecework_id=([0-9]+)(.*)
Sorry, that's quite right, but the basic idea is, if they put in the URL you like, Apache is ready to redirect to the real page without the browser seeing it.
Update
I'm way to sleepy to do regex correctly, so I had just tried my best to move your example around, sorry. I would try something more simple first just to get the basic concept down first. Try this:
RewriteRule www.mydomainname.com/en/piecework/piecework\.php\?piecework_id\=11 piecework.mydomainname.com/en/11
At the very least, it will be easier to see what isn't working if you get errors.