Subdomain redirect to URL Parameter while keeping other url params intact - .htaccess

I've looked like crazy for an answer to this - and I haven't been able to find one as of yet. My apologies in advance if I've missed something.
I'm looking to convert a subdomain to a url parameter while maintaining the other url pieces.
Such as:
http://sub.domain.tv/value redirects to http://domain.tv/value?campus=sub
This is what I've tried after looking through numerous posts on here:
# campus
RewriteCond %{HTTP_HOST} ^sub.domain.tv$ [NC]
RewriteRule ^(.*)$ http://domain.tv/$1/?campus=sub [R=301,L]
which worked fine for redirecting sub.domain.tv to domain.tv?campus=sub, but does not work with the rest of the URL parameters (so heading to sub.domain.tv/value does nothing).
I've tried other examples with the %1 and $1 being used as variables, such as with this post:
htaccess subdomain redirct with last url parameter but I don't understand the difference (and the examples I've tried did not work).
Thanks for taking a look! :) I very much appreciate it!

Have you tried the [QSA] parameter. This will add any remaining Query parameters to campus=sub

Related

I am not able 301 redirect domain.tld/?cur=usd to domain.tld

I try to redirect domain.tld/?cur=usd to domain.tld (there are many curencies, this is only example of one currency - we do not use anymore this solution).
I need to redirect only home with parameter to home without parameter. The other urls worked for me, I'm just having trouble getting work with that one.
I try to search and use online generators but none of the solutions work.
Here is what I am trying:
RewriteCond %{QUERY_STRING} (^|&)cur\=(.*)($|&)
RewriteRule ^$ /? [L,R=301]
// update
before this rule I have only
#bof redirects
RewriteEngine enabled
...and then there are redirects for other URLs, but I tested this rule separately first and the result was the same...
It not redirect me.
Thanks for the help and maybe an explanation of what I'm doing wrong.
RewriteCond %{QUERY_STRING} (^|&)cur\=(.*)($|&)
RewriteRule ^$ /? [L,R=301]
As mentioned in comments, this should already do as you require, providing there are no conflicts with other directives in the .htaccess file.
However, the regex in the preceding condition is excessively verbose for what you are trying to achieve (ie. just testing for the presence of the cur URL parameter).
If you simply want to check for the cur URL parameter anywhere in the query string then the regex (^|&)cur= would suffice (and is more efficient). No need to backslash-escape the literal =. And if the URL parameter always appears at the start of the query string then just use ^cur=.
I found the problem - it was something with the hosting, after a reboot everything started working as expected.
So I can confirm that this rule is fine.
Sorry for question.

htaccess from old forum url to new one

until now I have always used htaccess to rewrite URLS in order to have non-SEF url to SEF urls.
Today I am facing a new challenge that honestly, beeing non confident in regular expression, I really don't know how to achieve.
I have a situation where a forum on a website of mine has been update in the following form:
previous link: www.domain.com/forum3/topic/name-of-topic/post/7548
new link: forum.domain.com/Topic-name-of-topic/
How do I intercept /post/37764 string and tell htaccess to not consider it?
And how to instruct the server to build that kind of url instead of the provious. I am very confused about it.
Any suggestion? Thank you very much. Is there any resource that I can read to help me better understand the case?
Thanks again.
EDIT
Florian answer is correct. I just added few mods to fit it better.
RewriteEngine On
#RewriteBase /
RewriteRule ^forum3/topic/([^/]+)/post/[0-9]+$ http://forum.domanin.com/Topic-$1/ [L,R=301]
RewriteRule ^forum3/topic/([^/]+)-[0-9]+$ http://forum.domanin.com/Topic-$1/ [L,R=301]
You can try this code :
RewriteEngine On
RewriteBase /
RewriteRule ^forum3/topic/([^/]+)/post/[0-9]+$ /Topic-$1/ [L,R=301]
/([^/]+)/ means that we want to catch a string containing one or more characters except / preceded and followed by a /.
This link might help you to test your .htaccess files :
Test your apache htaccess files online

Apache mod_rewrite querystring problems

I have an old website - written in asp. The contact form uses a querystring to decide which form the visitor sees.
If a vistor wishes to just contact me, they may do so by clicking the link to:
http://example.com/contact.asp
If they wish to submit a fault - they do so by clicking the following link
http://example.com/contact.asp?problem=true&.......
Additional parameters are also passed after problem nb: ?problem=true&Customer... - however, I only need to know if problem=true
In the new site I have two pages: contact.html, and help.html
When a visitor goes to contact.asp they are redirected to contact.html - thanks to the following redirect rule:
RewriteRule ^contact\.asp$ http://example.com/contact-us.html [R=301,L]
However, I am having MASSIVE problems trying to get
http://example.com/contact.asp?problem=true to redirect to help.html
I've found tons of examples - but none of them work.. the page always redirects to contact.html
I have even commented out the above rewrite rule to see if my problem=true rule was being over-ridden.... This returned a 404. My problem=true rule just doesn't work.
I've been following examples such as: http://www.simonwhatley.co.uk/apache-htaccess-query-string-redirects
So to recap, I'd like to redirect contact.asp?problem=true to help.html, and contact.asp to contact.html
Many thanks in advance,
Rob
Not sure what you have tried as far as matching the query string, but the following should work.
RewriteCond %{QUERY_STRING} ^problem=true
RewriteRule ^contact\.asp$ http://primarysite.net/help.html [R=301,L]
RewriteRule ^contact\.asp$ http://primarysite.net/contact-us.html [R=301,L]
Hope that helps.

.htaccess Rewrite Based on Existence of Path in URL

Here's the scenario, I have a website that used to be a static HTML site and WordPress blog using a subdomain (http://blog.domain.com).
I recently combined everything into a single WordPress installation. To maintain old links I had to rewrite requests like "http://blog.domain.com/index.php/2010/10/16/post-name" to "http://domain.com/index.php/2010/10/16/post-name". My problem is that when trying to visit just "http://blog.domain.com", I get redirected to "http://domain.com" when I want it to go to "http://domain.com/index.php/blog".
So, if a user requests "http://blog.domain.com" (by itself, with or without slash), I want it to go to "http://domain.com/index.php/blog". If they request an old URL of "http://blog.domain.com/some-link-to-a-post", I want it to redirect to "http://domain.com/some-link-to-a-post". In other words, if it's a URL to an actual post, I just want to strip the "blog" subdomain. If it's the old link to the main blog page, I want to remove the "blog" subdomain and append "/index.php/blog"
http://blog.domain.com/ -> http://domain.com/index.php/blog
http://blog.domain.com/index.php/2010/10/16/post-title -> http://domain.com/index.php/2010/10/16/post-title
Hopefully that's clear. I'm not an htaccess expert, so hopefully someone can help me out here. Thanks in advance!
Using the [L] command at the end of a rewrite will tell htaccess that this is the last rule it should match. If you put a rule to match your first condition at the top and the other rewrite rule you said you had already created after it, you should get your expected result.
Try this:
RewriteRule ^blog.domain.com(/?)$ domain.com/index.php/blog [L]
# Your other rewrite here #
I couldn't get that solution to work. However, I used the following:
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/index.php/blog/$1 [R=301,L]
That ends up in a URL like http://domain.com/index.php/blog/index.php/2010/06/04/post-title, but Wordpress is smart enough to fix it.

.htaccess and url

Is it possible somehow using mod_rewrite change the url from http://www.mywebsite.com/company/123/reviews to http://www.mywebsite.com/company-123/reivews?
It's not a redirect. The problem is that the real path is the first one and I need my browser to display the second path. So when the user goes to company-123/reviews the content of the page is displayed from company/123/reviews.
Thank you.
RewriteRule ^/([a-z]*?)-([0-9]*?)/([a-z]*?)$ /$1/$2/$3
I think this will work, the regex does it's job atleast.
Use this rule to rewrite the former URL path to the latter one:
RewriteEngine on
RewriteRule ^([^/]+)-([0-9]+)/([^/]+)$ $1/$2/$3 [L]
But you already need the former URLs in your documents to get this rewriting work.

Resources