Redirect all subdomains to another file - .htaccess

My greetings to all.
I have a problem for which I am looking for a solution.
I have an my-website.com site and every user who registers on my site gets a personal link: USERNAME.my-website.com .
Meanwhile, I had the following rule in my htaccess :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.my-website\.com [NC]
RewriteRule (.*) process.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php/$1 [L]
How to make all sub-domains ( USERNAME.my-website.com ) point to process.php ( process.php/USERNAME ) file and that the others urls like my-website.com/OTHER point to index.php/USERNAME ? ( in case OTHER is not a file or a folder of my site )
Here's what I did, but it doesn't work.

"But it doesn't work" is a bit vague, so all we can do is given some general advice and suggestions...
First you need to make sure that those subdomains are actually served at all by your http server. You did not write anything about that, nor how you host your site at all.
Also you obviously have to take care that distributed configuration files (".htaccess") are considered at all by your http server, also something which is not clear from your question.
Given both of those aspects you will indeed have to concentrate on your rewriting rules. Here is a variant of your own implementation with a few changes:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/process\.php$
RewriteCond %{HTTP_HOST} ^(.*)\.my-website\.com [NC]
RewriteRule ^ /process.php/%1 [QSA,END]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/index\.php/?
RewriteRule ^/?(.*)$ /index.php/$1 [QSA,END]
You probably will have to make some more changes. But based on the vague information you gave it is hard to be more specific.
A good way to start debugging issues with rewriting is to enable rewrite logging inside your http server. That allows to closely monitor what is actually happening inside your rewriting engine. Please consult the documentation about that: https://httpd.apache.org/docs/current/mod/mod_rewrite.html

Related

rewriting url with .htacces cause files not to be found

In general, I am trying to understand how .htaccess works. I would highly appreciate it if anyone points me in the right direction. I have been trying to make the following url (with optional parameters) pretty.
mysite/v1.0/foldername
mysite/v1.0/foldername/param1/
mysite/v1.0/foldername/param1/param2/etc
I tried the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ foldername.php [QSA,L]
the problem is that when I get it to pass the parameters it can no longer get the resources. It seems to have changed directory.
.htaccess is in foldername
Also, I would like to know what site i can go to to learn about REQUEST_URI, REQUEST_FILENAME, etc. A site that is not too technical as it's the apache site.
You are incorrectly rewriting the rules correct rule according to your need would be like,
RewriteEngine On
# rule for removing extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w-]+)/?$ $1.php [QSA,L]
# below cond means incoming url is nor a file neither a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# actual rules
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?param1=$1 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ $1.php?param1=$2&param2=$3 [L]
Refrences
Reference: mod_rewrite, URL rewriting and "pretty links" explained
http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Remove .html extension from URL only in root

I'm trying to remove the .html file extension from my URL's to make them look nicer. I've seen many examples of this and have tried them, but I'm struggling to find something that only works in the root, and doesn't apply to any subdirectories or subdomains.
Can I get any help with this?
Example:
example.org/test.html > example.org/test
example.org/food/xyz.html > example.org/food/xyz.html
login.example.org/something.html > login.example.org/something.html
Your question is a bit vague, so I will concentrate on the main issue you mention:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/.+\.html
RewriteRule ^/?([^/]+)$ $1.html [L]
Alternative approach:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([^/]+)$ $1.html [L]
If this is limited to only a single host name (as your examples might suggest) depends on your setup. Such thing is easiest done if you place the rule in the real host configuration of your http server instead of using .htaccess style files. That is also why I append 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).
In the comment you wrote that you also want to force the shorter notation, so redirect users request a URL with such "file name extension" to the version without.
I don't really see any point in this, but it certainly is possible:
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !^.
RewriteRule ^. - [L]
RewriteCond %{REQUEST_URI} ^/.+\.html$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/?(.+)\.html$ /$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/.+\.html$
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([^/]+)$ /$1.html [L]

Multiple rewrite conditions for multiple parameters

There is an issue with the htaccess rewrite conditions in my setup.
Currently I have the following code.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://mydom.com/$1.php
This works fine for any base page making them look like this.
http://mydom.com/page
What I want to also be able to do is add parameters from the url if they exist. I have some pages that will be like this.
http://mydom.com/page?param=1&secondParam=2
What I've tried to do is add this.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)$ http://mydom/$1/$2/$3 [L]
RewriteRule ^(.*)/(.*)$ http://mydom/$1/$2 [L]
This made sense to me, because I thought if the condition didn't match, it would move on, but this gave me an internal server error.
What I ended up doing was setting up a separate rule for each page that could have multiple parameters like this.
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/(.)/(.)$ http://mydom.com/page.php?param=$1&secondParam=$2 [L]
RewriteRule ^page/(.*)$ http://mydom.com/page.php?param=$1 [L]
RewriteRule ^(.*)$ http://mydom.com/$1.php
This works, however you need to keep in mind relative links you may have in your site such as style sheets and javascript files. In my case, I had to replace all relative paths with full site paths, depending on the way you set up your site, it could take a while to replace.

htaccess, rewrite multiple pages to the same page but keep original value somewhere

I have been fiddle farting around with htaccess and RewriteEngine but I can't quite get my head around it...
I'm building a website on which I want users to be able to go to /portfolio/typography for example. But I don't want to create seperate pages for each category in this portfolio and thus I want to rewrite (redirect?) all the requests that go to /portfolio/ to the index.php of this directory and load the appropiate projects for this category from there.
Any ideas on how I could do this? I used this to redirect all the requests to /portfolio/:
RewriteCond %{REQUEST_URI} !/portfolio/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /portfolio/ [R=302,L]
Thanks in advance,
Cas Cornelissen
EDIT
Maybe I should note that I have another .htaccess file in the root of my website.
Ok, so I found the answer to my own question...
Seems like the following is working:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]

Do I need to call for .htaccess?

I have no experience with .htaccess, but I got a tip that it's very useful so I wanted to try this.
I now have a file called .htaccess, in my root folder.
The files contains this;
RewriteBase /
RewriteEngine on
RewriteCond %{HTTP_HOST} ^kellyvuijst\.nl [nc]
RewriteRule (.*) http://www.kellyvuijst.nl/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
What I'm trying to do here is create a 'www.mysite.com/portfolio/' instead of 'mysite.com/portfolio.html' I used some tutorials on this and I think it's correct, but I'm not sure.
So now I have this file, and what now? The tutorials all show what to put in the file but not what to do with it? Do I need to call for it in every .html page I have? And how do I call for it?
A .htaccess file is automatically invoked by the server.
You have just to put this into your file :
RewriteBase /
RewriteEngine on
RewriteRule www.mysite.com/portfolio/ /mysite.com/portfolio.html [L]
Hmm, you're using a lot of rules here to achieve just that.
Anyway, no you don't have to include that file. If you're hosting your site on a server with Apache it'll be included automatically. Can you also run PHP files or is your site just HTML? That's always an easy sign if you're also using Apache (not 100%, but often the go together).
If so, you could try just using these rules first:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.+)\.(.+)$ [nc]
RewriteRule ^(.*)$ http://www.%1.%2/$1 [R=301,L]
If that always adds www to your address, even if you type in the URL without www at least you can be certain that it works.
Then, to make the .html disappear you can add this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule $(.*)/$ /$1.html [L]
This should make every url that ends with a slash (like portfolio/) use a .html file instead (portfolio.html), but only if /portfolio/ isn't an actual directory on your website.
(I removed your url from the rules because this way it should also work if you use it on another website, or if you change your url. It should still do what you want)
Made sure the server is configured to allow htaccess files to override host options. So in your vhost/server config, you need:
AllowOverride All

Resources