Can't solve rewrite map and htaccess issue. Something wrong with condition? - .htaccess

I know this has to be something trivial I am missing. But I am going on a week here without any luck.
Say a user visits my old website with his username to get to his dashboard.
for example
foobar.example.com
would direct the visitor to
example.com/dashboard/12345
where 12345 is the userid for foobar user.
This has been working for the longest time.
However, now I moved everyone to a new server, new software and new user id's
And I have this htaccess setup
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ https://www.newexample.com/profile/${zq3:$1|0} [L]
The zq3 usermap query simply does ...
RewriteMap zq3 "dbd:SELECT newuserid FROM rewrite_map WHERE username = %s"
The query works fine. However for some reason, when the map query is called it always returns 0 which is the if not found return this .
I believe something is wrong with the rewrite rule where it is not passing the username properly.
Any help would be appreciated. Oh Also, would like to ignore www if that is passed.

Might be like this?
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ https://www.newexample.com/profile/${zq3:%1|0} [L]
%1 is the first match from RewriteCond, not $1 as it's the first match from RewriteRule.
Please see: https://httpd.apache.org/docs/2.4/rewrite/intro.html#regex

Related

basic htaccess redirect by with variable move from URL (?id=1e34-2)

Ahoy all, I know these questions have been beaten to death. And even after my hour of research on stack, none of the suggestions or answers I have attempted have worked.
The only thing I am needing to do is....
Take the variable from a URL and redirect to another page (site), inserting that variable accordingly.
Keep in mind the variable can contain letters, dashes, underscores or numbers.
Original
/directory-here/a-random-name.php?strPropID=13d3-1
and I am redirecting to another domain.
http://www.someotherdomain.com/redirect.php?strPropID=13d3-1
So now I have this:
RewriteRule ^/directory-here/a-random-name.php\?strPropId=(.*)$ http://www.someotherdomain.com/redirect.php?propid=$1 [R=301,L]
However the redirect isn't working. What am I doing wrong here? I know it has to be something simple, no?
Thanks in advance!
You need to match query string using a RewriteCond not in RewriteRule:
RewriteEngine On
RewriteCond %{THE_REQUEST} /directory-here/a-random-name.php\?strPropId=([^\s&]*) [NC]
RewriteRule ^ http://www.someotherdomain.com/redirect.php?propid=%1 [R=301,L,NE]

htaccess sport links redirects

Need to redirect set of hundred or so links from one domain to another. This is my current code (not working):
RewriteCond %{HTTP_HOST} ^www.onedomain.info/$1/staticword($2.*) [nc]
RewriteRule (.*) http://otherdomain.com/$1/staticword($2.*) [R=301,L]
Redirect domains themsleves is a no-brainer and that's correct I think, then I too think that $1 is correctly - cuz $1 is a variable for 12 different words for sport categories (like soccer or hockey), sometimes there is one word, sometimes the other (but ofc it should be the same, so this is why I have that $1 there - correct me if I am wrong but this could work I think...).
Problem is that after that there is one static word which is not changing (is same all the time in every link - it's something like "watch"...) BUT after that word there can be absolutely ANYTHING which I tried to solve by ($2.*) but it's wrong for some reason.
Can you help please? Thanks!
RewriteEngine On
# HTTP_HOST if to match domain names only
RewriteCond %{HTTP_HOST} ^www\.onedomain\.info$ [NC]
# On the left hand we match the rest and on the right we redirect
RewriteRule ^(.*)/(staticword.*)/?$ http://otherdomain.com/$1/$2 [R=302,L]
Note that I am using 302, because you want to test it first before change it to 301 so your browser does not get cached with it until you are sure it's working as you want it to.
So given your example http://onedomain.info/soccer/watchfe27789-mexico-vs-trinidad-and-tobago-gold-cu‌​p, the rule would be like this:
RewriteEngine On
# HTTP_HOST if to match domain names only
RewriteCond %{HTTP_HOST} ^www\.onedomain\.info$ [NC]
# On the left hand we match the rest and on the right we redirect
RewriteRule ^(.*)/(watchfe27789.*)/?$ http://otherdomain.com/$1/$2 [R=302,L]

Change the url parameters of incoming link

If I a user comes to the site
http://subdomain.example.com/faq/index.html
how to I pass it to the server as
http://subdomain.example.com?q=subdomain.com/faq/index.html?
The key is that I want the url to still look like the first URL, so I wouldn't want to redirect it.
I am also using php but I don't think I can accomplish this with it.
Assuming that the second ? is really just the question mark from the original question, you can capture the query string components like this:
RewriteBase /
RewriteCond %{HTTP_HOST} ^([A-Z0-9-]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ /?q=%1.com/$1 [L]

Mod_Rewrite for htaccess

The URL structure for Joomla 1.5 changed in version 2.5.
Before it was:
http://example.com/index.php?option=com_content&task=view&id=587&Itemid=73
Now it's:
http://example.com/index.php?option=com_content&view=article&id=587&Itemid=114
*Note the id and itemid numbers change based on page and while all pages have an id not all have the Itemid in the url.
While we updated all links in the database lots of people still have the old link structure so we want to edit the htaccess file so if someone enters the old structure it will forward to the right structure.
Simply put any URL entered at example.com with task=view in the url should be replaced with view=article.
Does anyone know a simply way to do this in htaccess, maybe with replace query string method and 301 redirect?
You can match against the query string using mod_rewrite's RewriteCond and the %{QUERY_STRING} variable. Then use the % backreferences in a RewriteRule
Try something like this:
RewriteCond %{QUERY_STRING} ^(.*)&task=view&(.*)$ [NC]
RewriteRule ^(.*)$ /$1?%1&view=article&%2 [R=301,L]

htaccess subdomain rewrite

It is many topics here about subdomains but no one can help me...
I use htacces to set subdomain to folder
So if we put http://en.example.com/something
I use something like this..
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ http://example.com/%1/$1 [NC]
This works fine but adress in bar is changed to http://example.com/en/something but I want keep http://en.example.com/something
so I tried this
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^([^.]+)\.example\.com(.*) /$1/$2
or just
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ %1/$1 [NC]
but this doesn't work. Any solution or ideas ?
One solution is use language there (http://example.com/en/something) where I rewrite it but after If I work on subdirectories I get something like http://example.com/subdirectory/en/something - terrible.
Maybe I like http://example.com/en/subdirectory/something bud how proceed this...
And also on some private servers first one case send me to "maybe" default domain, so it is not working for me. (maybe this is some server condition or settings)
I know this is a month late, but maybe this will still be useful for somebody. A few things here:
Regarding your first RewriteRule:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ http://example.com/%1/$1 [NC]
As it seems you already discovered, rewriting to another URL will also redirect the user's browser to that new URL, even if it's at your own domain. To keep it hidden, you have to rewrite to a file path on the server (like you do with your next two rules).
Regarding your second RewriteRule:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^([^.]+)\.example\.com(.*) /$1/$2
The problem there is that you can't match the domain name in the RewriteRule, only the URL path. If your URL is www.example.com/something/somethingelse, the string you're trying to match is just something/somethingelse. In other words, it excludes www.example.com/, so this RewriteRule pattern you have will never match the domain name because the pattern isn't even being tested against that part of the URL, but you included the domain name in the pattern, causing the match to fail.
Regarding your third RewriteRule:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ %1/$1 [NC]
This looks like it should work, so I can't say for sure why it isn't without knowing more about how your files are organized on the server and so forth. Let's say you have all of the website's files in /home/somebody/public_html/. In order for the RewriteRule to work as it is right now, you would need to have an en subdirectory in public_html. So, if somebody went to en.example.com/something, the RewriteRule would cause Apache to serve the file at /home/somebody/public_html/en/something. My guess why it's not working for you is that you might have the subdomain pointing somewhere other than public_html (assuming you actually had the website files organized like in my example). Remember that what you're rewriting to (/$1/$2 in this case) is a file path on the server, not a URL to your website.
I hope that helps! You may have already solved this by now, but even if you have, I'm hoping other people will still find this useful.

Resources