rewriting a substring of a filename in RewriteRule - regex

I am trying to use the .htaccess code to have different pages loaded when mobile user-agent recognized.
It doesn't work but I can't figure why, can someone please help?
The below RewriteConds work for me when followed by the RewriteRule for redirecting to subdomain, so I guess there must be a problem with my RewriteRules.
Help appreciated.
RewriteCond %{HTTP_USER_AGENT} android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge\ |maemo|midp|mmp|opera\ m(ob|in)i|palm(\ os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows\ (ce|phone)|xda|xiino [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a\ wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r\ |s\ )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1\ u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp(\ i|ip)|hs\-c|ht(c(\-|\ |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac(\ |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt(\ |\/)|klon|kpt\ |kwc\-|kyo(c|k)|le(no|xi)|lg(\ g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-|\ |o|v)|zz)|mt(50|p1|v\ )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v\ )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-|\ )|webc|whit|wi(g\ |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-) [NC]
RewriteRule ^regular.css$ mobi.css
RewriteRule ^view.$ mobile.view.

You writing ^view.$ supposes that you think of a file named "view." (it's the complete filename, ending with a dot). Is that really what you mean?
$ marks the end (of the original string the regex is being matched against). Perhaps, you don't need it.
(Read docs about regexes before you use them in order to understand every symbol you write! Writing code and not understanding what it means is not nice.)
Further problems
I see, after you get rid of the $, you might get further problems, because you might want to construct a complete filename on the right-hand side of the rewrite-rule (like something that eveluates to "mobile.view.mainlayout.php") (or not?).
I don't remember: does Apache's rewrite-rules rewrite only the small matched piece ("view.") in the string and concatenate it with the rest of the requested filename ("mainlayout.php"), or Apache throws away the old requested filename ("view.mainlayout.php") and replaces it with what it finds on the right-hand side of the rewrite-rule (so it must be not a replacing piece like "mobile.view.", but rather evaluate to a complete filename)?
If so, then match the rest of the string with ( ) in the regex on the left-hand side of the rewrite-rule, and insert the matched piece back on the right-hand side.

Main problem is that you cannot have multiple RewriteRules after RewriteConds, so your second RewriteRule will be executed every time.
You have to do a little workaround with the skip flag, see below.
Note: The RewriteConds are 'inverted'.
The second problem is your RegEx: RewriteRule ^view.$ mobile.view. just rewrites the URL view (followed by one random char) to the URL mobile.view..
As I noticed in your comment, you have to do something like this:
RewriteRule ^view\.(.*)$ mobile.view.$1 # files and .htaccess have to be in the same directory
Here the full Rewrite code:
RewriteCond %{HTTP_USER_AGENT} !android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge\ |maemo|midp|mmp|opera\ m(ob|in)i|palm(\ os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows\ (ce|phone)|xda|xiino [NC]
RewriteCond %{HTTP_USER_AGENT} !^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a\ wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r\ |s\ )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1\ u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp(\ i|ip)|hs\-c|ht(c(\-|\ |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac(\ |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt(\ |\/)|klon|kpt\ |kwc\-|kyo(c|k)|le(no|xi)|lg(\ g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-|\ |o|v)|zz)|mt(50|p1|v\ )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v\ )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-|\ )|webc|whit|wi(g\ |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-) [NC]
RewriteRule .* - [S=2] # skip next two RewriteRules if RewriteConds matched (= it's a desktop browser)
RewriteRule ^regular.css$ mobi.css
RewriteRule ^view\.(.*)$ mobile.view.$1

#Freelancer [Edited to fix errors and provide alternative]
You want a [PT] on your RewriteRule statements.
Skipping is probably your best bet:
RewriteCond "%{HTTP_USER_AGENT}" "!(first-condition-regex)" [NC]
RewriteCond "%{HTTP_USER_AGENT}" "!(second-condition-regex)" [NC]
RewriteRule "^" "-" [SKIP=2]
RewriteRule "^regular.css$" "mobi.css" [PT]
RewriteRule "^(view\..*)" "mobile.$1" [PT]
If you want to avoid inverting your conditions it gets a bit messier:
RewriteCond "%{HTTP_USER_AGENT}" "first-condition-regex" [OR,NC]
RewriteCond "%{HTTP_USER_AGENT}" "second-condition-regex" [NC]
RewriteRule "^" "-" [E=MOBILE:TRUE]
RewriteCond "%{ENV:MOBILE}" "!=TRUE"
RewriteRule "^" "-" [SKIP=2]
RewriteRule "^regular.css$" "mobi.css" [PT]
RewriteRule "^(view\..*)" "mobile.$1" [PT]
As pointed out, all these files need to be in the same directory as the .htaccess file.

Related

.htaccess rewrite rule with multiple {QUERY_STRING}s

I am working on a website project where we basically move from TYPO3 to a WordPress & Magento-solution.
Before launching the new site, I would like to add rewrite rules to point the old (TYPO3, non-SEF) URLs to the corresponding new ones. I have an Excel-list with around 1000 URLs that I somehow would like to add to htaccess and create 301's.
If you have a better approach for this, I'd be thankful.
What I am struggling with is:
The "old" URL structure looks something like ?id=123\&user_e15proddb1_pi1[domain]=42
the correcponding new URL would be
/de/alle-produkte/neuheiten.html
RewriteEngine is on, RewriteBase is /.
I tried
RewriteCond %{QUERY_STRING} ^id=123\&user_e15proddb1_pi1[domain]=42$
RewriteRule . /de/alle-produkte/neuheiten.html [R=301,L]
With additionally escaping the _and the [] with no avail.
I tried to seperate the {QUERY_STRING}s into two by
RewriteCond %{QUERY_STRING} ^id=123$
RewriteCond %{QUERY_STRING} ^user_e15proddb1_pi1[domain]=42$
followed by TheRule. Also no avail.
Rewriting itself works, because I tried
RewriteRule .id=123\&user_e15proddb1_pi1\[domain\]=42$ /de/alle-produkte/neuheiten.html [R=301,L]
But that only works without the question mark in the beginning.
Could you give me a hint on what I am doing wrong?
You can use this rule by escaping [ and ]:
RewriteCond %{QUERY_STRING} ^id=123&user_e15proddb1_pi1\[domain\]=42$
RewriteRule ^ /de/alle-produkte/neuheiten.html? [R=302,L]
Also note ? at the end of target URI to strip off any existing query string to prevent a redirect loop.
I got a solution to and made this one work:
RewriteCond %{QUERY_STRING} ^id=123\&user\_e15proddb1\_pi1\[domain\]=42$
RewriteRule (.*) /de/alle-produkte/neuheiten.html? [R=301,L]
Proably something with escaping those characters was going wrong when I tried over and over again.

What does the '?' symbol mean in a .htaccess rewrite condition?

I have been reading the Rewrite condition info in the Apache documentation but have got nowhere, so I hope you can help. In my .htaccess file I had some lines that ran thus:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
I have since noticed that a dollar sign was missing from line 3, so it should have run thus:
rewritecond %{http_host} ^mydomain.com$ [nc]
So my question has two parts, was it working (doing anything) originally (and what??)? And if not, how was it interpreted with the missing '$'. Thank you.
Your question says ? but probably mean missing anchor $ from your regex.
rewritecond %{http_host} ^mydomain.com$ [nc]
Even more correct would be:
rewritecond %{http_host} ^mydomain\.com$ [nc]
Since dot can mean anything in regex and literal dot needs to be escaped.
Without $ also it might work but in theory it can match mydomain.comp also. With $ in place it can only match mydomain.com

Rewrite rule in htaccess for nice links and I need advice

I'm trying to make nice links in my apps.
I decided to rewrite links that look like these:
1. http://example.cz/get1/get2/get3
2. http://example.cz
Into these (I have php appliactions only):
1. http://example.cz/index.php?path=get1+get2+get3
2. http://example.cz/index.php?path=
I'm removing www before links.
I keep failing to rewrite it into .htaccess.
I'm also looking for advice if the primary idea of rewriting get params into path=get1+get2+get3 is good? Right now I can see that link like this http://www.example.cz/you+me/ could possibly fail somewhere. Do you have any better solution?
So question is: How to rewrite it into .htaccess and how to solve possible problems with link that contains '+'
EDIT:
I improved my skills a little and I did this:
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule (.*) http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^\/index.php(.*)
RewriteRule ^(.+) /index.php?path=/$1 [R=301,L] # 301 is here so I can see how does it work
# everything above works well (as I want)
# link now look like this one:
# http://example.net/index.php?path=/get1/get2/get3
# Now I was looking for universal rule that will rewrite my get params...
# First I did this:
RewriteCond %{REQUEST_URI} /([^/]+)
RewriteCond %1 !index.php(.*)
RewriteRule /([^/]+) $1+ [R=301,L]
# If any part of request uri is string that matches pattern /([^/]+)
# And if ([^/]+) doesn't match index.php(.*)
# then rewrite every /([^/]+) into $1+
# Now I see that it is incorrect, but I wasn't able to fix it
# So then I did different rule
RewriteRule ^([^=]+=[^\/]*)\/([^\/]+)(.*)$ $1$2+$3 [R=301,L]
# start of string
# first var is: one or more chars except =, =, zero or more chars except /
# /
# second var is: one or more chars except /
# third var is: zero or more chars
# end of string
I think second idea was much more better, but it doesn't work too. Please help me to fix it.
You can do this with the Apache module mod_rewrite. Chances are you probably already have it installed. Try this:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?path=$1+$2+$3 [L]
This regex assumes the URL will always contain three groups of text between slashes. You can tweak it as needed.
Also note that Apache never sees the URL hash, so you won't be able to match it in a rewrite rule. Luckily, it looks like you don't want to do anything with it anyway. Just use the rule above, and the hash will remain at the end of the URL in the browser.
I did solution. Problem was that after adding index.php?path= I wasn't able to work with query string...
The final universal solution that turn links from http://www.example.net/get1/get2/get3 to
http://example.net/index.php?path=get1+get2+get3:
RewriteEngine on
RewriteBase /
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule (.*) http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^\/index.php(.*)
RewriteRule ^(.+) /index.phppath=/$1 [R=301,L]
RewriteRule ^([^=]+=[^/]*)/([^/]+)(.*)$ $1$2+/$3 [R=301,L]
RewriteRule ^(.*)\+/+$ $1 [R=301,L]
RewriteRule ^(.*)path=(.*)$ $1?path=$2 [R=301,L]

mod_rewrite trailing slash for directory and remove for file url

Ok. I got this problem I trying to remove the last slash in a file url for example http://domain.com/styles/styles.css/. I got the code for adding slash to the end but cannot figure how to do the conditional.
If the URL has an extesion then remove end slash
else add slash..
Here what I got right now some blogs says its the solution but still isn't working for what I expect.
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
Also a problem, when I type http://domain.com/index it goes to http://domain.com/inde/.
Need your help guys.. Thanks a lot in advance.
add following code in your htaccess, for better understanding.
RewriteCond %{HTTP_HOST} !^\.yourdomain\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
I have a link : domain.com/folder/
it will change to : domain.com//folder
also You can turn off mod_dir's redirect by including a DirectorySlash Off.
Why do you want to do an external redirect for such "furniture" files? Surely an internal redirect is what you want here?
Options -MultiViews
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^(.+)/$ $1 [L]
I advise that you turn off mutliviews if you don't use it as this can generate subrequests which confuse things.
Your RewriteCond conditions are logically inverted because you have the ! operator there. So the rewrite is applying only for those inputs which do not have extensions, and which do not have a trailing slash!
You can do this with a single rule with no conditions:
# Match any sequence of characters, ending in a dot followed
# by one or more characters that don't contain dots or slashes,
# followed by a final trailing slash.
#
# Rewrite this absolutely and treat as redirect.
RewriteRule ^(.*\.[^./]+)/$ /$1 [L, R=301]

mod_rewrite / redirection with ? in a query string

I'm diving into the uses of htaccess/mod rewrite for the first time and I'm having a little bit of trouble with a redirect/mask.
I have a test directory in my root folder called modrw, in that folder is a index.php file with a nice and simple:
<?php echo $_GET['name']; ?>
In the browser if I type www.domain.com/modrw/{word}/ then the word is echoed on the page, which is what I want.
If I type www.domain.com/modrw/name={word} then I am redirected to www.domain.com/modrw/{word}/ and the word is also echoed as I intended.
However, if I direct the browser to the URL www.domain.com/modrw/?name={word}/ the word is echoed but I am not redirected to www.domain.com/modrw/{word}/ like I hoped.
How is the ? causing troubles? In the RewriteRule code below the ? is included, I've tried it a couple different ways but can't get it working.
Here is my htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
RewriteRule ^modrw/?name=([^/\.]+)/?$ http://www.domain.com/modrw/$1 [R]
RewriteRule ^modrw/([^/\.]+)/?$ modrw/?name=$1
What is causing the problem, is there a specific way to include the ?, does it not pick this up at all? Am I taking completely the wrong approach?
I've also tried using Options +FollowSymlinks but I'm not entirely sure what this does nor if it's needed at all...
Remember that RewriteRuleonly matches REQUEST_URI. To match query string you must use RewriteCond with variable %{QUERY_STRING}. For ex. in your case:
RewriteCond %{QUERY_STRING} ^name=(.*)(&|$) [NC]
RewriteRule ^modrw /modrw/%1? [L,R,NC]