I had a laravel project on a sub-domain and I migrated to another sub-domain.
I want to redirect all requests from old sub-domain to the new one.
For example:
https://old.website.com
https://old.website.com/login
https://old.website.com/contact
Should become:
https://new.website.com
https://new.website.com/login
https://new.website.com/contact
I moved all the files to another folder and added .htaccess with the following code:
RewriteEngine On
RewriteRule ^(.*)$ https://new.website.com/$1 [R=301,L]
But the issue is that public is added to the url for example:
https://old.website.com
Becomes:
https://new.website.com/public
So I get 404 not found error.
For the sub-domain I set the document root to point to public folder so no need to add the public directory to the url.
By the sounds of it, you likely have an internal rewrite to the /public subdirectory. Try the following instead:
RewriteRule ^ https://new.website.com%{REQUEST_URI} [R=301,L]
Test with 302 (temp) redirect first to avoid potential caching issues. You will need to clear your browser cache before testing.
I have this solution on one of my projects. To redirect requests from old sub-domain to the new one without the public folder in the URL, you could need to modify the .htaccess file. You can try the following code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old.website.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old.website.com$
RewriteRule (.*)$ https://new.website.com/$1 [R=301,L]
This should redirect all requests to the new sub-domain while preserving the rest of the URL. This way, you should be able to access the pages at their new URL without encountering a 404 error.
Related
I'm trying to deploy my Laravel app and block the access to the others files like .env
I put all my laravel app in the www folder, and I add this htaccess :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
But when I go to my domain url I have all the files.. seem like my htaccess is not working (he's on the Laravel app root)
Here's a simple method using only a .htaccess file placed in Laravel's root directory - e.g. alongside app, bootstrap, config, ... No changes whatsoever are necessary to your code.
The file rewrites all the requests so that requesting /file.png would in fact return /public/file.png and anything else is routed to /public/index.php. This also ensures that nothing outside the public folder can be accessed, thereby protecting any sensitive files like .env or database/*.
The simple method
This method assumes that DOCUMENT_ROOT is set properly by your Apache server. Try this first and use the 2nd method only if it doesn't work on your server.
.htaccess
RewriteEngine on
# serve existing files in the /public folder as if they were in /
RewriteCond %{DOCUMENT_ROOT}public%{REQUEST_URI} -f
RewriteRule (.+) /public/$1 [L]
# route everything else to /public/index.php
RewriteRule ^ /public/index.php [L]
The slightly more complicated method
If your server doesn't set DOCUMENT_ROOT properly, then you'll need to use an absolute path in RewriteCond. That is, an absolute path on the server's filesystem. You can get it by copying the following script to the directory where your Laravel installation will reside and visiting its URL - i.e. http://example.com/get_doc_root.php.
get_doc_root.php
<?php
echo getcwd();
This should result in something like /var/www/example.com/web. Use the following .htaccess file and replace [[your path]] with the actual path you got.
.htaccess
RewriteEngine on
# serve existing files in the /public folder as if they were in /
RewriteCond [[your path]]/public%{REQUEST_URI} -f
RewriteRule (.+) /public/$1 [L]
# route everything else to /public/index.php
RewriteRule ^ /public/index.php [L]
In our example case, the RewriteCond line would look like this:
RewriteCond /var/www/example.com/web/public%{REQUEST_URI} -f
My hosting provider doesn't allow me to upload files on root. It only give access to public folder.
I tried to move index.php & .htaccess files from Project's public directory to Server's Public Directory. It temporarily solve the problem. But I have to change every single file structure. Otherwise it doesn't find link. So please suggest me to upload my Laravel Project on such Host.
Try this in your .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
Of course, replace domain-name.com with your actual domain.
What this does is, it internally forwards all requests to your public folder. For example www.domain-name.com/lorem-ipsum will be redirected to www.domain-name.com/public/lorem-ipsum.
However, I have not tested this but it should work well in theory.
I'm a complete Codeigniter noob.
I think I have everything setup properly, and the pages display when I go to:
http://www.example.com/index.php/pages/view/my_page
but I get an Apache 404 error when I visit without the index.php file:
http://www.example.com/pages/view/my_page
Is there anything obvious I'm missing? .htaccess rules, or a change in the controller?
By default, the index.php file will be included in your URL
You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Situation: I have a single (main) domain, which has several aliased domains, each of which are pointing at the same Plesk-based server (for instance, I have example.com as main, with something.net, anotherone.co.uk, and several others all as aliases of the main domain account). This means that whenever I enter the domain name into my address bar of any of the aliases, it goes directly to the account of the main domain (example.com).
Problem: Based on the domain name of the alias(es), I have an index.php that redirects each domain differently (for instance, requests to domain A redirects to a corporate site, domain b goes to a thanks site etc.) Which works great, but if a directory is added after the domain URL (i.e. somealias.com/something) then it gives a 404 not found error.
What I would really appreciate, if someone can help me out, is a (single if possible) rewrite ruleset that would essentially strip off ALL trailing directories and/or GET requests, and only leave the typed-in base URL, so then the php script sitting in the main domain document root can take over and deal with the request appropriately.
Strangely enough, I've not been able to find a (simple) solution for this anywhere. Is it a case of having to define a rule for each of the aliased domains individually?
Try the following,
#Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.olddomain.com$[OR]
RewriteCond %{HTTP_HOST} ^olddomain.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !=/
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule .* http://%{HTTP_HOST}/? [L]
This will take ALL requests except root folder / (e.g. http://example.com/) or index file (e.g. http://example.com/index.php) and redirect them to the root folder (e.g. http://example.com/some-url will be redirected to http://example.com/).
You may need to replace index.php by the file that is get executed when you hit the root folder (Apache will silently rewrite http://example.com/ to http://example.com/index.php (depending on your actual settings) as it needs to have a file to execute otherwise it may show an error).
Alternatively (possibly even better -- depends on your actual setup and requirements) you may use these rules -- this will redirect only non-existing URLs. So if you have an image meow.png on your site, these rules will allow you to access it (http://example.com/meow.png):
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* http://%{HTTP_HOST}/? [L]
UPDATE:
If you going to place this into config file (httpd-vhost.conf or httpd.conf) then use these rules:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule .* http://%{HTTP_HOST}/? [L]
It seems to me that all the sites are hosted on the same server (probably using the same code base).
If your index.php is a front controller you can redirect everything to your index.php and decide in the first lines of index.php what front controller to load (like backend.php).
If you don't mind having to maintain a list of the aliases you can define a hash of [alias] => path-to-front-controller.
In the front controller of you main domain you check the alias name (using $_SERVER['SERVER_NAME'] for example) against the hash and load the appropriate file.
You will have to add and entry to the hash each time you add anew alias. If they are not generated dynamically maintaining this hash is not a lot of hassle.
I want to create folders on the fly, but make it seem like I am creating subdomains on the fly using mod_rewrite. e.g.
Create "john" folder using PHP
www.example.com/john/
Then be able to access whatever I put in there at:
john.example.com
Thank you in advance,
Kris
First you need to configure your server to accept any subdomain for your domain example.com and redirect it to your virtual host that as well has to accept any subdomain. After that, you can use the following rule to rewrite that subdomain internally to a folder with the same name:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule ^ %1%{REQUEST_URI} [L]