how to dynamically rewrite filepath .htaccess based on domain? - apache

I have this apache rule in my .htaccess file:
RewriteCond %{HTTP_HOST} ^testURL.localhost.local
RewriteRule ^index.php$ /_testURL/index.php [L]
How would I write this so that testURL2.localhost.local or any other subdomain would be rewritten to the corresponding directories?
For instance testURL2.localhost.local would be rewritten to _testURL2/index.php etc
I already tried the option below but I didn't get the intended result:
RewriteCond %{HTTP_HOST} ^testURL
RewriteRule ^cms.php$ /_%1/cms.php [L]

With your shown samples, attempts; please try following htaccess rules file. I have posted 2 sets of htaccess rules file here, you have to use ONLY one set at a time.
1st solution: This is specifically for host testURL.localhost.local and will look for either index.php OR cms.php only in UI.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?(testURL)\.localhost\.local$ [NC]
RewriteRule ^((?:index|cms)\.php)$ _%1/$1 [NC,L]
2nd solution: A Generic solution, where it will look for anyvalue.localhost.local and it will add anyvalue in path while rewriting, also it will look for any php files in uri.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]*)\.localhost\.local$ [NC]
RewriteRule ^([^.]*\.php)$ _%1/$1 [NC,L]
NOTE1: Also please make sure to clear your browser cache before testing your URLs.
NOTE2: Please keep these rules at the top of your htaccess rules file.
NOTE3: I am also additionally/optimally matching www. in host in case you don't want it you could remove it (?:www\.)? part in condition.

Related

htaccess rewrite root url to subfolder with accessing other folder?

I have following folder structure in apache server.
Public_html
-->admin
--->admin_login.php
-->website
--->index.php
since the index.php inside the website folder,
i have given following code in .htaccess
RewriteCond %{REQUEST_URI} !^/website/
RewriteRule ^(.*)$ /website/$1 [L,NC]
so that the when the user enter root url , it will appear "www.myurl.com" instead of "www.myurl.com/website/"
but the issue is, i could not be able to access admin_login.php.
is there anyway to modify .htaccess, to come website/index.php in main url and able to access admin_login.php(both)?
Thanks in Advance
You need to add an exception to existing rule like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(website|admin)/ [NC]
RewriteRule .* website/$0 [L]
Negative condition %{REQUEST_URI} !^/(website|admin)/ will match every URI except URIs that start with /website/ or /admin/. This will allow you to directly open www.myurl.com/admin/admin_login.php.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php/?$ website/index.php [QSA,NC,L]

.htaccess redirect to subdomain url with hash

I'm trying to redirect a site with the following url structure:
https://example.com/2021/about
to
https://2021.example.com/#about
https://example.com/2021/visit
to
https://2021.example.com/#visit
how can i do this?
i tried adding this to the /about directory in the original domain:
RewriteEngine on
RewriteBase /
RewriteRule (.*) https://2021.example.com/#about [R=301,NE, L]
but what i got after the redirect was
https://2021.example.com/#about2021/about
which is not right. any help is appreciated
[EDIT] i only want to apply this to some folders, like /2021/about and 2021/visit
You may use this redirect rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com) [NC]
RewriteRule ^(20\d{2})/(.+?)/?$ https://$1.%1/#$2 [R=301,L,NE]
Make sure this is your topmost rule in .htaccess and you clear your browser cache before testing this new rule.
Could you please try following, written based on your shown samples. Please make sure to clear your browser cache before testing your URLs. Following will only be applied to 2021/about OR 2021/visit uris.
RewriteEngine ON
RewriteCond %{HTTP_HOST} (example\.com) [NC]
RewriteRule ^(2021)/(about|visit)/?$ http://$1.%1/#$2 [R=301,NC,NE,L]

Redirect URL (including port) with params using .htaccess

I am trying to redirect from
http://www.example.com:81/my/api/search?query=test
to
http://www.example.com:81/my/php/api.php?query=test
using
RewriteCond %{QUERY_STRING} ^query=(.*)$
RewriteRule ^api/search(.*)$ /php/api.php?%1 [L]
However, it doesn't work for me. Additionaly for testing htaccess rules I use http://htaccess.madewithlove.be/.
Possibly even better would be to check if request starts from ^api and it's GET type (I guess using RewriteCond %{REQUEST_METHOD}) then redirect to /my/php/api.php?[query params here]
Anyone can point me into right direction?
If your .htaccess is located in /my/ directory then you can use:
RewriteEngine On
RewriteBase /my/
RewriteCond %{QUERY_STRING} ^query=.
RewriteRule ^api/search/?$ php/api.php [L]
QUERY_STRING will be automatically carried over to target URL.

apache RewriteRule not working

I have this url http://www.example.com/Courses/get/38789/my-course, i added this rule to the .htaccess file
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/Courses/get
RewriteRule ^Courses/get/(.*)/(.*)$ course-$1-$2 [R=301,L]
but when i go to http://www.example.com/Courses/get/38789/my-course nothing happens, i stay on the same page, there is no redirect.
p.s the link is just an example not the actual link
A more efficient method would be to use the following:
RewriteRule ^Courses/get/(\d+)/([^/]+)/?$ /course-$1-$2 [R=301,L]
Now, keep in mind that this rule should come before any rules that may rewrite the request to, say, an index.php file. This would be naturally true if the code you posted in your question was all of your code. If not, please post your entire .htaccess file so we can be sure it is being placed in the right location.
Be sure the mod_rewrite is turned on, and the you have set AllowOverride All in your virtual host/Apache site configuration. If you're running on a shared production server, this would not apply to you.
Side note: Whilst it does work, you need not use RewriteEngine on twice - only once at the beginning of your file will suffice. You also do not need RewriteCond %{REQUEST_URI} ^/Courses/get - it is essentially redundant as you are already using an expression to test against the request.

Proper htaccess rewrite

I'm trying to force an old URL to go to the new url and my code seems to have no effect
RewriteCond %{QUERY_STRING} ^index2\.php?page=shop\.product_details&\.tabs\.tpl&product=310&category=71&Itemid=2$
RewriteRule .* /hunting/back-packs/multi-packs/black-2.5-pack.html [R=301,L]
my old url is
www.mywebsite.com/index2.php?page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2
and my new one is
www.mywebsite.com/hunting/back-packs/multi-packs/black-2.5-pack.html
My code does not break anything but does not work either
Thanks in advance
As I understand you want redirect (301 Permanent Redirect) so the URL will change in browser. This will work for this URL ONLY /index2.php?page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2:
RewriteCond %{QUERY_STRING} =page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2 [NC]
RewriteRule ^index2\.php$ http://www.mywebsite.com/hunting/back-packs/multi-packs/black-2.5-pack.html [R=301,L]
If you want internal redirect (rewrite), then use these lines:
RewriteCond %{QUERY_STRING} =page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2 [NC]
RewriteRule ^index2\.php$ /hunting/back-packs/multi-packs/black-2.5-pack.html [L]
PLEASE NOTE:
You need to put these lines in a proper order (order of rules matters) otherwise (if you put it at the end) some another rule will rewrite it to a different URL.
This needs to be placed in .htaccess file in website root folder. For any other location you may need to modify it a bit.