simple(?) htaccess rewriting issues - apache

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

Related

.htaccess -> Characters not escaping in rewrite string "(" ")"

I'm currently trying to add some redirects from a very old site that had PDFs and some of these PDFs have used brackets to contain the years.
For example:
/document%2070C11-name%20(2012).pdf
So the full URL I am trying to redirect is:
https://website.co.uk/document/1/document%2070C11-name%20(2012).pdf
I have previously setup many different redirects on this site but ones with the special characters seem to be causing an issue.
Here is the examples of what I have tried, and failed with so far:
RewriteEngine on
RewriteBase /
RewriteRule ^document/1/document%2070C11-name%20\(2008\).pdf$ document/newurl/ [R=301,L]
RewriteRule ^document/1/document%2070C11-name%20(.*)2008(.*).pdf$ document/newurl/ [R=301,L]
So I tried escaping the character and just trying a wildcard. Neither seemed to work on my Apache server. The code I used did seem to work when I tested it on:
http://htaccess.madewithlove.be/
But I am pretty stuck now and would love any help I can get.
Thanks,
Kane
To match %MN character in URL, you need to use \xMN in the RewriteRule pattern.
Hence this rule should work for you:
RewriteRule ^document/1/document\x2070C11-name\x20\(2008\)\.pdf$ /document/newurl/ [R=301,NC,NE,L]
This will redirect https://website.co.uk/document/1/document%2070C11-name%20(2008).pdf to https://website.co.uk/document/newurl/

apache 2.2.22 mod_rewrite RewriteCond, how to set the regexp

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.

htaccess rewrite hide .php and also flatten query string on one particular url

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

Apache Rewrite - put parts of query string in replacement string

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.

Complex htaccess rewrite rules - where do i start?

I have asked on here before about mod_rewrite and got the perfect answer but now I need to do something more complex and really needed the advice of someone who knows mod_rewrite much better.
My rewrite rule looks like this so far:
RewriteRule ([a-zA-Z_-]+)/([0-9]?) index.php?cat=$1&page=$2
And that shows the URL as follows: /categoryname and shows pages like /categoryname/1/.
But I would really like to modify the rule to work with sub categories such as /categoryname/subcategoryname/ and still work with the page system (subcat/2/) but I just can't seem to wrap my head around this.
It needs to ignore the sub cat rewrite if you are on the main category etc.
Hoping someone can help me.
RewriteRule [a-zA-Z_-]+/([a-zA-Z_-]+)/([0-9]?) index.php?cat=$1&page=$2 [L]
RewriteRule ([a-zA-Z_-]+)/([0-9]?) index.php?cat=$1&page=$2 [L]
I would use these rules:
RewriteRule ^([a-zA-Z_-]+)/([a-zA-Z_-]+)/(([1-9][0-9]*)/)?$ index.php?cat=$1&subcat=$2page=$4
RewriteRule ^([a-zA-Z_-]+)/(([1-9][0-9]*)/)?$ index.php?cat=$1&page=$3
With the assertion for the start (^) and the end of the string ($) there is no ambiguity to have one of those rule accidentally applied. And the (([1-9][0-9]*)/)? part is for the optional paging.