Always rewrite a baseurl to /my-page - .htaccess

For many reasons, I need to rewrite my Base URL to a page, which is a hub.
i.e. http://data.mydomain.com always directs the user to http://data.mydomain.com/my-page
What's the best way to do this?

Use this
RewriteCond %{REQUEST_URI} !^/my-page
RewriteRule ^(.*)$ /my-page/$1 [L,R=301]

Did you try anything?
/?$ /my-page

Add this in you htaccess :
RewriteEngin on
RewriteCond %{HTTP_HOST} ^data.mydomain.com$
RewriteRule ^/?$ /my-page [L,R=301]

Related

301 redirect from URL with GET-parameters to the homepage

I have an old website with Joomla 1.5. It has some strange links with GET-parameters, like this:
http://www.primavista.ru/images/stories/catalog/?rand=1186511674
http://www.primavista.ru/images/stories/catalog/?rand=145388433
http://www.primavista.ru/images/stories/catalog/?rand=1553907057
http://www.primavista.ru/images/stories/catalog/?rand=1563973527
http://www.primavista.ru/images/stories/catalog/?rand=1981273478
http://www.primavista.ru/images/stories/catalog/?rand=2139631800
http://www.primavista.ru/images/stories/catalog/?rand=366928750
http://www.primavista.ru/images/stories/catalog/?rand=524689684
http://www.primavista.ru/images/stories/catalog/?rand=569077423
http://www.primavista.ru/images/stories/catalog/?rand=573405687
http://www.primavista.ru/images/stories/catalog/?rand=879649167
I want make redirect theses links to the homepage.
I tried some different instructions in .htaccess:
RewriteCond %{QUERY_STRING} ^/images/stories/catalog/?rand=([0-9]*)$
RewriteRule ^(.*)$ https://primavista.ru/? [R=301,L]
RewriteCond %{QUERY_STRING} ^/images/stories/catalog/?rand=(.*)$
RewriteRule ^(.*)$ https://primavista.ru/? [R=301,L]
RewriteCond %{QUERY_STRING} (^|&)(rand)=[^&]+ [NC]
RewriteRule ^images/stories/catalog(/.*)?$ https://primavista.ru/? [R=301,L,NC]
But no one not working. Maybe here someone can help me with this. Thanks
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} rand=\d+
RewriteRule ^/?images/stories/catalog/?$ / [R=301,L]
It is a good idea to start out with R=302 temporary redirections and to only change that to R=301 permanent redirections one you are satisfied with everything. That prevents nasty caching issues on the client side ...
UPDATE:
Your comment below indicates that you actually ask to remove the GET parameter in the redirected request, which you never mentioned before...
You can use the additional QSD flag for that:
RewriteEngine on
RewriteCond %{QUERY_STRING} rand=\d+
RewriteRule ^/?images/stories/catalog/?$ /? [R=301,QSD,L]

Redirect url with get variables use .htaccess

It is possible to redirect in .htaccess
this url
http://test.com/uploads/image.jpg?w=200
to this
http://test.com/public/uploads/image/200.jpg
?
I need this to cache system in my rest API.
I'm not sure that is possible rewrite get variable in this way.
Cheers
Check this rule, maybe it help.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^uploads/image.jpg
RewriteCond %{QUERY_STRING} w=(.*)
RewriteRule ^(.*)$ /public/uploads/image/%1.jpg? [R=301,L]

redirect with htaccess and parameters

I moved from a wordpress to a typo3 site. On my old sites, the links looked like this:
http://www.example.org/?page_id=44
now I want to redirect it to:
http://www.example.org/contact
Usually the redirects-rules are no problem, but this time I don't get it why it is not working:
I tried this:
Redirect 301 /?page_id=44 http://www.example.org/contact
as well as this:
RewriteRule http://www.example.org/\?page_id=44 http://www.example.org/contact [R=301,L]
and this here:
RewriteCond %{REQUEST_URI} ^example.org/\?page_id=44
RewriteRule ^(.*)$ http://www.example.org/contact [R=301,L]
Tried it in several browsers and incognito-mode, but it still remains wrong, it still adds the parameter instead of redirecting to the certain page.
I guess it is somehow doable with %{query_STRING} ?
Is it due to the param-thingy?
Any ideas how to solve this problem?
Okay, got it. It had something to do with the query string:
Final result:
RewriteCond %{QUERY_STRING} ^page_id=14$ [NC]
RewriteRule ^(index\.php){0,1}$ /kontakt/? [L,R=301,NC]
You can use:
RewriteCond %{THE_REQUEST} /\?page_id=44\s [NC]
RewriteRule ^/?$ /contact? [R=301,L]

how to redirect from modules.php?name=News&file=article&sid=XXX to domain.eu/XXX?

I would like to redirect all url requests with the phpnuke string modules.php?name=News&file=article&sid=XXX (xxx is a number) to my domain-name.eu/XXX . How is that possible?
i tried
RewriteCond %{QUERY_STRING} ^(.*&|)modules.php?name=News&file=article&sid=(&.*|)$
RewriteRule ^(.*)$ %1domain.eu/%2 [L,R=301]
but it has no effect. Where is the mistake?
Also i don't understand why a simple
RewriteRule ^(.*)$ /modules.php?name=News&file=article&sid=$1 [L,R=301]
doenst work. Any ideas?
Thanks a lot for any help.
Not sure why I'm seeing this in Drupal questions. But try this.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/modules\.php$
RewriteCond %{QUERY_STRING} ^sid=([0-9]*)$
RewriteRule ^(.*)$ http://domain.eu/%1 [R=301,L]
You can't match everything after the sid=(.*) regex part in the URL in most cases. modules.php?name=News&file=article&sid=XXX and modules.php?sid=XXX&name=News&file=article are the same in functionality.
Learned at http://www.simonecarletti.com/blog/2009/01/apache-query-string-redirects/

Htaccess Redirect

I am looking to make www.purchase.example.com redirect to purchase.example.com, below is an example of what I am trying to do:
RewriteCond %{HTTP_HOST} ^www\.purchase\.
RewriteRule (.*) http://purchase.DOMAIN_NAME/$1 [L,R]
I need a variable that will replace DOMAIN_NAME with simply purchase.example.com.
Obviously I can hard code the purchase.example.com but I will need the code to work on multiple sites. Any suggestions?
For your knowledge:
I used a RewriteCond backreference:
RewriteCond %{HTTP_HOST} ^www\.purchase\.(.*)
RewriteRule (.*) http://purchase.%1/$1 [L,R]
I would not do this in code, I would do this on the web hosting account.
If you need it a little more generic redirect for every domain starting with www.:
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule ^ http://%0%{REQUEST_URI} [L,R=301]

Resources