I want created a wildcard subdomain for each user from mysql database. The subdomain works but the page is loading without the related files (css, js).
List of user page :
website.com/user/index.php
User page :
website.com/user/userpage/user.php?username=john
This is my rewrite code:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.website.com/user/user.php$
RewriteCond %{HTTP_HOST} ^(.*?).website.com$
RewriteRule (.*) user.php?username=%1
Is there anything wrong with my rewrite code? . Thanks :D
EDIT
Forgot one thing this is my wildcard *.website.com directory :
public_html/user
Thanks :D
This line looks wrong:
RewriteCond %{HTTP_HOST} !^www.website.com/user/user.php$
The %{HTTP_HOST} variable is meant to match the hostname only, and not the path (/user/user.php); besides, you should escape dots (which are reserved characters in regexes), so you should change it to:
RewriteCond %{HTTP_HOST} !^www\.website\.com
You may be interested in this page of the Apache docs.
Related
I have a subdomain and I want to redirect to the main domain (using .htaccess) like so:
https://abc.example.com I want to redirect it to https://www.example.com
https://abc.example.com/path/page-name to https://www.example.com/path/page-name
https://abc.example.com/path/page-name?test=12&test1=12 to https://www.example.com/path/page-name?test=12&test1=12
Please suggest how I can do it.
I have already tried the below solution but its not working.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC]
RewriteRule ^ http://www.example.com/suberror [L,R]
I am using Laravel.
At this line :
RewriteRule ^ http://www.example.com/suberror [L,R]
There is no pattern means , regex checked against requested URI .
Change it to this :
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R]
This part ^(.*)$ is pattern and it will be presented in substitution by this $1
If it is Ok , Change [L,R] to [L,R=301] to be permanent redirection because R alone means R=302 which is temporary .
Assumptions:
You have 1 subdomain (as stated in your example)
The subdomain and main domain point to the same area on the filesystem (they share a common root).
The try something like the following at the top of your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^abc\.example\.com [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R,L]
The URL-path from the request is held in the REQUEST_URI server variable. The query string from the request is passed through to the substitution (target) without any additional work.
Been trying to resolve this problem with a rewrite rule that assigns a subdomain to a root directory of the same name, for example.
ddd.example.com will link to "/_projects/ddd" directory, that works fine and I have no trouble with it, the issue is that any files or directories I have in the root directory "/" can be accessed from the subdomain ddd.example.com.
Here is an example directory structure
example.com = "/"
"index.php"
ddd.example.com = "/_projects/ddd"
no files
So if for instance I access ddd.example.com/index.php, it will resolve to using the file located example.com/index.php which is located a directory below.
Here is the rewrite rule for .htaccess
# Skip rewrite if subdomain is www
RewriteCond %{HTTP_HOST} !^www\. [NC]
# Extract (required) subdomain to %1
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com(:80)?$
# Redirect to domain if requested URL does not resolve to existing subdirectory path
RewriteCond %{DOCUMENT_ROOT}/_projects/%1 !-d
RewriteRule (.*) http://example.com/ [NC,R=301]
# Skip rewrite if subdomain is www
RewriteCond %{HTTP_HOST} !^www\. [NC]
# Extract (required) subdomain to %1
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com(:80)?$
# Skip rewrite if requested URL does not resolve to existing subdirectory path or file
RewriteCond %{DOCUMENT_ROOT}/_projects/%1/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/_projects/%1/$1 -d
RewriteRule (.*) /_projects/%1/$1 [NC,L]
What if your RewriteConds fail? Then the URL falls through and is not rewritten. And so it accesses the document root. I would just create separate VirtualHost entries for every single supported subdomain. (How many are there?)
Suppose the client asks for http://sub.example.com/index.php.
Suppose that there exists an /_projects/sub/index.php.
Your RewriteCond-s will see that /_projects/sub/index.php exists as a file, and then skip the rewrite. But if the rewrite is skipped, then there is no redirect to /_projects/sub/. So what document is fetched in that case? You guessed it, /index.php.
You should unconditionally redirect these subdomains to their proper places (subject only to checks against looping).
Why did you split the rewrite into two, one doing an internal redirect? The internal redirect isn't rewriting the whole URL to example.com, and so it stays in the subdomain. It looks like you can get into a loop there.
My attempt at rewriting was to do this essentially.
Pseudo Code:
if (subdomain-directory != exists)
redirect them to the home page
else
rewrite the request for the subdomain
I could only accomplish that using two rules, I haven't found any other way to accomplish this, so this was my attempt.
The condition in question actually works fine, if I have an index.php in the /_projects/sub directory then it will use that file and the same for any other file I put in there.
I have absolutely no idea how I can accomplish this with mod_rewrite, I have played around with it for the best part of a few weeks to no avail, searched endlessly for possible solutions and have not made any progress.
Resolved the problem, seems that there was a looping problem that was breaking the rewrite.
##### Subdomain to subfolder
# Fix missing trailing slashes.
#RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
#RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
#RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d
#RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
# Rewrite sub domains.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ /projects/%2/$1 [QSA,L]
I have a web app which serves images based on the subdomain.
We wanted to provide our users with a url like this:
http://{username}.domain.com/images/myimage.jpg
Instead of what we used to have:
http://www.reallylongdomainname.com/users/{username}/images/myimage.jpg
This makes the url shorter and less 'snoopable'.
So I set up an IIRF .ini file to do some url rewriting and it works great except for the fact that some of our users folders have an underscore. And from what I've read, underscore is not a valid character in a domain name (even though IIS supports it).
I want to know how I could do a find and replace in the $1 back reference so that a url like this:
http://some-user.domain.com/...
Could be rewritten to this:
/users/some_user/..
Here's my IIRF rule.
RewriteCond %{HTTP_HOST} ^(?!www)([^\.]+)\.domain\.com
RewriteRule ^/(.*)$ /users/*1/$1 [L,I]
Thanks for any help.
If you know there is no more than x dashes in the username:
# no dash
RewriteCond %{HTTP_HOST} ^(?!www)([^\.\-]+)\.domain\.com
RewriteRule ^/(.*)$ /users/*1/$1 [L,I]
# one dash
RewriteCond %{HTTP_HOST} ^(?!www)([^\.\-]+)-([^\.\-]+)\.domain\.com
RewriteRule ^/(.*)$ /users/*1_*2/$1 [L,I]
# two dashes
RewriteCond %{HTTP_HOST} ^(?!www)([^\.\-]+)-([^\.\-]+)-([^\.\-]+)\.domain\.com
RewriteRule ^/(.*)$ /users/*1_*2_*3/$1 [L,I]
It's not beautiful, but it works.
Days later I asked about redirecting dynamic directories to index.php, and I got this code that works perfect (it's the only code I have in .htaccess):
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*) index.php
This translates urls like http://mydomain.example/dynamicdir/ to http://mydomain.example/index.php
Now I want to translate subdomains like http://dynamicdir.mydomain.example to http://mydomain.example/index.php?dir=dynamicdir
From examples I found in Internet I tried adding this line:
RewriteRule ^(.*)\.mydomain\.example index.php?dir=$1
But it doesn't work. I don't have enough experience with mod-rewrite to tell what's missing or wrong. Could you please help me to find a way to keep the dynamic directory translation, and add the catch-all subdomain rule?
Regards!
The mod_rewrite rules use the request path, which is relative to the virtual host. Try having different rewrite rules for each virtual host, but placing their documents in the same directory.
With RewriteRule you can only test the URL path. For the host name, you need to use %{HTTP_HOST} in a RewriteCond:
RewriteCond %{HTTP_HOST} ^(.+)\.example\.example$
RewriteRule ^ index.php?dir=%1
I think this is a pretty straight forward question in mod_rewrite:
I got one domain, which needs to redirect to another, but keep any value after last slash (/) in the first URL, over to the second.
domain.com/4433 should transfer to domain.com/folder/?p=4333
Listed for clarity:
From: domain.com/4433
To: domain.com/folder/?p=4333
Any ideas?
Edit:
Did some testing, we found the following solution:
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^([0-9a-z]*)$ /folder/?p=$1 [NC]
sincerely,
- bakkelun
In case you don't really want to redirect but to have pretty URLs, you can use
RewriteEngine On
RewriteRule ^/(.+)$ /folder?p=$1 [L]
This takes everything after the first slash and inserts it at the $1 - but only if there's something after the slash. It doesn't issue a redirect so the users won't notice.
Without any further information, try this:
RewriteEngine on
RewriteRule ^/([^/]+)$ /folder/?p=$1
If you want to use the rule in a .htaccess file, remove the leading slashes.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ domain.com/folder?p=$1 [R=301,L]
Just in case: domain.com = domain1.com and domain2.com? domain1.com should be redirected to domain2.com? Both run on the same server (optional)?
[EDIT:]
If you really only want to do the thing as stated in the comment, then do the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^4433$ http://domain2.com/folder/?p=4433 [R=301,L]
Else, as Benedikt Eger said, or with R=301 if you want real redirection.
Or, if you want it to redirect only on numbers, then do the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^([0-9])+$ http://domain2.com/folder/?p=$1 [R=301,L]
RewriteCond checks, if defined vhost is domain1.com, but not domain2.com, then the rewrite rule is applied, and redirects via HTTP status 301 [R=301] only number strings (0-9)+ consisting of at least one number to the specified URL. [L] makes this the last rule applied.