Redirecting to another domain with query strings - .htaccess

I am moving to another domain and I would like to redirect dynamic and static ULRs too. I have tried many .htaccess redirect rules but they don't work. For example:
FROM: http://www.bercsekft.hu/motorosbolt/index.php?id=546
TO: https://www.bercsemotor.hu/k/robogo-alkatreszek/aprilia-alkatreszek/aprilia-kormany-alkatresz
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteCond %{QUERY_STRING} ^id=509$
RewriteRule ^(.*)$ https://www.bercsemotor.hu/k/robogo-alkatreszek/aprilia-alkatreszek/aprilia-kormany-alkatresz [R=301,L]
I have also another rules in .htaccess. I am not sure if they have affect to the redirect rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^bercsekft.hu$
RewriteRule ^(.*)$ "http\:\/\/www\.bercsekft\.hu\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^bercsekft\.hu$ [OR]
RewriteCond %{HTTP_HOST} ^www\.bercsekft\.hu$
RewriteRule ^jarmu\/?$ "http\:\/\/www\.bercsekft\.hu\/motorosbolt" [R=301,L]
RewriteCond %{HTTP_HOST} ^bercsekft\.hu$ [OR]
RewriteCond %{HTTP_HOST} ^www\.bercsekft\.hu$
RewriteRule ^edeny\/?$ "http\:\/\/konyhafelszereles\.bercsekft\.hu\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^bercsekft\.hu$ [OR]
RewriteCond %{HTTP_HOST} ^www\.bercsekft\.hu$
RewriteRule ^edenybolt\/?$ "http\:\/\/konyhafelszereles\.bercsekft\.hu\/" [R=301,L]
RewriteBase /
RewriteRule ^sitemap.xml$ motorosbolt/sitemap2.php [L]
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php52” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php52___lsphp .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

Loos like your issue is the location of that dynamic configuration file you use (".htaccess") in combination with the paths you defined in there. If you really need to use a dynamic configuration file then it usually is easiest to place it in the http servers DOCUMENT_ROOT folder. You need to make sure that such files are considered at all by the http server (hint: the AllowOverride directive) and that the file is readable by the http server process.
This is a slightly modified version of your code to adjust the paths, also I replaced the numerical ID, your question contradicts itself in that detail:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)id=546(&|$)
RewriteRule ^/?motorosbolt/index\.php/?$ https://www.bercsemotor.hu/k/robogo-alkatreszek/aprilia-alkatreszek/aprilia-kormany-alkatresz [R=301]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Related

Advence .htaccess redirect with one exception

I need help with .htaccess redirect.
There is a lot of simmilar answers, but did not find the one for my case.
I ve got this config:
<IfModule mod_rewrite.c>
RewriteEngine On
# Rewrite to index.php if not file or directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Now I need redirection for all url but not for 'news_list'
This work for all redirections:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ https://www.newsite.com/$1 [R=301,L]
</IfModule>
For exception I should use:
RewriteRule !^news_list($|/) https://www.newsite.com/$1 [R=301,L]
This works - 'news_list' do not redirect, but problem is, that than '/news_list' doesn't work (because it need point to 'index.php?url=news_list'.
So I need kinda mix of all configs above, but no luck.
Thank you in advance!
This probably is what you are looking for:
RewriteEngine On
# Rewrite everything to new host except for /news_list...
RewriteCond %{REQUEST_URI} !^/news_list(?:$|/)
RewriteRule ^ https://www.newsite.com/$1 [R=301,END]
# Rewrite to index.php if not file or directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)$ /index.php?url=$1 [QSA,END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Redirect 301 htaccess for specific domain

I try several things for my redirections but without working solution.
I have 2 domains pointing in the same folder/website (mulitshops prestashop) and I would like to redirect some page from the first domain on a specific page and keep domain.
My work :
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.local$ [NC]
RewriteRule ^/fr/blog/inspirations-c4$ https://domain1.local/fr/journal [R=301,L]
RewriteRule ^/fr/blog/inspirations-c3$ https://domain1.local/fr/journal [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2.local$ [NC]
RewriteRule ^/fr/blog/inspirations-c4$ https://domain2.local/fr/test [R=301,L]
RewriteRule ^/fr/blog/inspirations-c3$ https://domain2.local/fr/test [R=301,L]
But I cant do this with simple Redirect 301 because I can't specify the domain from request URI.
Beause the target page it's not the same according to the domain.
Sorry for my english and thank you in advance for your help.
In addition to what #DusanBajic explained in his comment to your question you also need to consider the difference between absolut and relative path in rewriting rules. This is actually explicitly documented...
WHen implemented in distributed configuration files (".htaccess") the rule pattern is matched against the relative path of the requested URL. You however try to match it against an absolute path which will never match. So either change your patterns to use relative paths too, or, preferably, implement your rewriting rules such that they work in both cases. So also when the rules are implemented in the real http server's host configuration where the pattern is matched against the absolute path inside the requested URL. This appears confusing at first. But it does make total sense, once you think about it.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c4$ https://domain1.local/fr/journal [R=301,L]
RewriteCond %{HTTP_HOST} ^domain1\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c3$ https://domain1.local/fr/journal [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c4$ https://domain2.local/fr/test [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c3$ https://domain2.local/fr/test [R=301,L]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out..
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

htaccess links with query strings not opening on pc,

my htaccess file looks like this:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap.php$ sitemap.xml [L]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
I want my links look like:
https://kaznews.kz/news/477800
as they are at the time,
but when I have QUERY STRING they not opening links like: https://kaznews.kz/news/477800?google
I want either delete the ?mark and the query, or add them at the end but show the correct page.
RewriteRule ^(.*)$ /index.php?/$1 [r=301,L,QSA]
this is not suitable for me because it gives me
such result: https://kaznews.kz/index.php?/news/477800&google with index.php inside, but there will be duplicate links then.
Well, looks like that is roughly what you are looking for:
RewriteRule ^/?news/(\d+)/(.+)$ /news/$1?$2 [END,QSD]
That rule will internally rewrite requests to /news/477800/google to /news/477800?google.
Update:
From your comments below we learned that what you actually appear to ask is how you can remove, so ignore any query arguments specified in the request. Though this is a questionable thing to do (as reasoned in the comments) here is a rule to achieve that:
RewriteRule ^/?news/(\d+)$ /news/$1 [END,QSD]
General:
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

How to redirect everything except root and some folders in htaccess

I am looking for .htaccess code that will redirect all traffic at mydomain.com to example.com/folder1 except the root of example.com and folder2, folder3 and folder4.
Summery:
Url to redirect - all files and folders at example.com
Redirect to - to example.com/folder1
Exception - root, folder2,folder3,folder4
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteCond %{REQUEST_URI} !^/folder1(/|$)
RewriteCond %{REQUEST_URI} !^/folder2(/|$)
RewriteCond %{REQUEST_URI} !^/folder3(/|$)
RewriteCond %{REQUEST_URI} !^/folder4(/|$)
RewriteRule ^$ https://example.com/folder1 [L,R=301]
EDIT:
Tried to use the above code to redirect but not sure if the code itself has issues because it doesn't seem to work as expected. What could be the problem?
The Problem: I want the redirection to exclude home page, folder2, folder3, folder4 otherwise redirect everything to to folder1
Thanks to your comments and the additional information you gave the question becomes more clear now.
I suggest this rule set, it should get you closer to what you try to achieve, though it still might need some tweaking...
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/folder1/?
RewriteCond %{REQUEST_URI} !^/folder2/?
RewriteCond %{REQUEST_URI} !^/folder3/?
RewriteCond %{REQUEST_URI} !^/folder4/?
RewriteRule ^/?(.*)$ https://example.com/folder1/$1 [END,R=301]
This rule set will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and in case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

htaccess redirect index to public ignore css img and js

I have a .htaccess file in my server to rediect my subdomain website index folder to public.
However, after the redirection, css, js, img file all cannot be load.
How to fix it?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.subdomain.domain\.com.sg$
RewriteRule ^/?$ "http\:\/\/subdomain.domain\.com.sg\/" [R=301,L]
DirectoryIndex public/index.php
Edited :
I have update my htaccess file to this.
the index file is loaded correctly, but can i remove the "public" from the url?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.subdomain.domain\.com.sg$
RewriteRule ^/?$ "http\:\/\/subdomain.domain\.com.sg\/" [R=301,L]
RewriteRule ^index.php$ subdomain.domain.com.sg/public/index.php [R=301,L]
sorry I am new to apache environment
With "can i remove the "public" from the url?" you probably ask if you can internally rewrite incoming requests to the /public folder. Certainly you can. Question is if you want only that index.php file to be redirected or all requests beneath that. Note that these rules implement an internal rewriting, so do not alter the URL visible in the browser which is most likely what you want:
# http://subdomain.example.com/index.php => /public/index.php
RewriteRule ^/?index.php$ /public/index.php [L,QSA]
A more general approach:
# http://subdomain.example.com/foo/bar => /public/foo/bar
RewriteRule ^/?(.*)$ /public/$1 [L,QSA]
If you register index.php in your http servers host configuration as index document, then you can even drop that from the URL. Documentation: https://httpd.apache.org/docs/current/mod/mod_dir.html#directoryindex:
# http://subdomain.example.com/ => /public/index.php
DirectoryIndex index.php
And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Resources