.htaccess Subdomain Rewrite (not like other questions!) - .htaccess

I wish to rewrite admin.example.com/test/ to admin.example.com/index.php?site=test.
I have done ample research and have yet to be able to do so. Regex is not my thing, anyone have suggestions?
(in this particular example, only 'test' is a variable (i.e. 'admin' is constant'))
Thanks!

You could use mod_rewrite to do so:
RewriteEngine on
RewriteRule ^([^/]+)/$ index.php?site=$1
And if you want to restrict that rule to admin.example.com, add this condition:
RewriteEngine on
RewriteCond %{HTTP_HOST} =admin.example.com
RewriteRule ^([^/]+)/$ index.php?site=$1

Related

how to Rewrite Single Page url in .htaccess

I need to rewrite only 1 specific URL
from
http://www.domainname.com/index.php?route=payment/axis/callback
to
http://www.domainname.com/payment/axis/callback
I tried these two from stack overflow, I don't know why its not working
1st one :
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?route=payment/axis/callback [NC,L]
2nd one :
RewriteRule ^index.php?route=payment/axis/callback payment/axis/callback [L]
Try this:
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
See the full page here.
Hope it helps!
I wouldn't use .htaccess with RewriteRule, since there are often problems with it. A simple workaround (with PHP redirect):
<?php
if($_GET['route'] == 'payment/axis/callback') {
header("Location: http://www.domainname.com/payment/axis/callback");
}
?>
You can either use h0ch5tr4355's workaround or you can try this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^route=payment/axis/callback$
RewriteCond %{SCRIPT_FILENAME} index.php
RewriteRule (.*) /payment/axis/callback [NC,QSD]
If you instead of a rewrite would like it to redirect to the new url you can add R=301 to the flags of the RewriteRule.

RewriteMap not working at all

I'm trying to get MapRewrite working for some vanity urls but I'm just not having any luck. I don't get errors, it just doesn't seem to work (redirect).
Here's the code I put in my vhost.conf:
RewriteEngine On
RewriteMap vanURL txt:/var/www/vhosts/myconditions.txt
RewriteCond ${vanURL:$1|not-found} ^(.+)$
RewriteCond %1 ~^not-found$
RewriteRule ^/(.*) /${vanURL:$1|/$1} [L]
What I'm looking to do is to determine if "www.mydomain.com/some_folder" exists. If it doesn't, look in "myconditions.txt" for "some_folder" and redirect to the corresponding location.
Here's an example of MyConditions.txt
some_folder another_folder
some_folder_two another_folder_two
Visiting www.mydomain/some_folder is simply a dead link.
Can anyone point me in the right direction?
(Note that I did test putting garbage in my Vhost.conf and .htaccess to ensure the files are being read)
You cannot use %1 in LSH of the condition, use a negative lookahead like this:
RewriteEngine On
RewriteMap vanURL txt:/var/www/vhosts/myconditions.txt
RewriteRule ^/([^/]+)(/.*)?$ /${vanURL:$1}$2 [PT]

.htaccess redirect from one subfolder to other subfolder

I know this sounds like so many other questions here, but I can't seem to find the answer.
Say you are on:
www.domain.com/folderA/folder2/folder3/
I want that to redirect to:
www.domain.com/folderB/folder2/folder3/
So the whole structure stays the same.. it just redirects.
Now so far I have:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/folderA [NC]
RewriteRule ^(.*)$ /folderB/$1 [R=301,L]
But when I use that, it'll just do
www.domain.com/folderB/folderA/folder2/folder3/
What am I doing wrong? How do I get rid of that folderA?
The pattern ^(.*)$ includes also the prefix folderA. You must specify folderA explicitly in the pattern and capture only the latter part in the RewriteRule. Then you can drop the RewriteCond
RewriteEngine on
RewriteRule ^/?folderA/(.*)$ /folderB/$1 [R,L]
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

How to create virtual username.domain.com using .htaccess mod rewrite

I know there are plenty of questions like I asked, but searching all day the soluation, I didnt find right one that would work for me, therefore I asked question.
Here is .htaccess code I have to view user profiles:
RewriteRule ^users/([^/\.]+)/$ viewProfile.php?user=$1&%{QUERY_STRING}
RewriteRule ^users/([^/\.]+)/([^/\.]+)/$ viewProfile.php?user=$1&usr_profile=$2&%{QUERY_STRING}
And using this rewrite users profile URL is:
http://www.domain.com/users/username.html
Inside of this type of URL, I would like to create subdomain users URL, like:
http://username.domain.com
Can anyone suggest the solution?
Thanks a lot.
Make sure your vhost is setup to accept requests for *.domain.com. Then add this above your other rewrite rules:
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteRule ^$ viewProfile.php?user=%1 [L,QSA]

.htaccess redirect "domain.tld/" to "domain.tld/home.html"

I have a website here on my localhost :
http://localhost/mysite/www/index.php
I have some RewriteRules to redirect like this :
http://localhost/mysite/www/index.php?page=home
-> http://localhost/mysite/www/home.html
And now, I want to do a redirection like this :
http://localhost/mysite/www/
-> http://localhost/mysite/www/home.html
I have an environment variable named REWRITE_BASE containing /mysite/www/. So what I thought to do was to compare {REQUEST_URI} to %{ENV:REWRITE_BASE} ... like this:
RewriteCond {REQUEST_URI} =%{ENV:REWRITE_BASE}
RewriteRule . %{ENV:REWRITE_BASE}home\.html [R=301,L]
But it don't works well.
To help you understand what I want to do, here is the working code in PHP to do what I want:
$rewriteBase = getenv('REWRITE_BASE');
if ($rewriteBase === $_SERVER['REQUEST_URI'])
header('Location: '.$rewriteBase.'home.html');
Thanks for help.
Okay... so, as I can't use a variable for my comparison, here is the way that I made it works :
# Redirect domain.tld/ to domain.tld/home.html
RewriteCond %{REQUEST_URI} =/www/ [OR]
RewriteCond %{REQUEST_URI} =/mysite/www/
RewriteRule ^(.*)$ %{REQUEST_URI}home\.html [R=301,L]
# RewriteBase equivalent - Production environment
RewriteRule . - [E=REWRITE_BASE:/www/]
# RewriteBase equivalent - Development environment
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule . - [E=REWRITE_BASE:/mysite/www/,E=DEVELOPMENT_ENV:1]
# Website rewritings
RewriteRule ^([^/]*)(?:/([^/]*))?\.html$ %{ENV:REWRITE_BASE}index\.php?page=$1&view=$2 [QSA,L]
Now it's alright. Thanks for your answers! ;)
What you want to do won't work, because mod_rewrite doesn't expand any variables present in its test patterns. Therefore, when you do something like this...
RewriteCond %{REQUEST_URI} =%{ENV:REWRITE_BASE}
...What you're actually doing is comparing the value of %{REQUEST_URI} to the string "%{ENV:REWRITE_BASE}", instead of the value "/mysite/www/" like you wanted. Is there a reason that you can't specify the value directly in your RewriteCond or simply move the rules so that they're relative to the /mysite/www/ directory to begin with?
There might be another way to approach the problem, but unfortunately (and for some good reasons) you cannot perform that kind of comparison using mod_rewrite.

Resources