I'm working on a small site and I've got a question.
How can I remove the /public and the singular name from url? However for the entries I want to keep the first segment.
How do I do that? Because when I try to rewrite it in the htaccess the assets break (images etc)
You need to point your server to the /public directory. Here is what the official documentation is saying:
Only the 'public' folder needs to be accessible in the browser. After the first installation this folder is named public/ but as you read on, you will see that you can rename it to www/ or whatever your web server requires. To do this, configure your webserver to use the public/ folder as the web root. For more information about this, see the pages on configuring Apache or Nginx.
Source: https://docs.bolt.cm/3.2/installation/install-command-line
The directive for Apaches VirtualHost is (in your example domaincom.conf in /etc/apache/sites-enabled:
<VirtualHost *:80>
DocumentRoot /your-bolt-directory/public
</VirtualHost>
And for Nginx:
location / {
root /your-bolt-directory/public;
}
Use .htaccess file to rewrite your URL's:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/page [L]
</IfModule>
The public part of the url should not be visible if you have your Apache virtualhost setup correctly.
Ordinarily, all you need to do here is point the DocumentRoot setting to the public directory, so for example if your setup currently looks like this:
DocumentRoot /home/mysite
You adjust it to:
DocumentRoot /home/mysite/public
You may also need to do the same for any <Directory /home/mysite > commands that are in your config too.
Once you have amended, restart Apache and http://yourdomain.com/ should load up the homepage and http://yourdomain.com/bolt take you to the backend.
Related
Sorry for asking this question again. Even though this has been asked and discussed quite a bit I cannot seem to find the right solution for a local dev environment using VirtualHost. I am using XAMPP Portable for Windows for dev work but assume this is the same for any other local server with regards to the .htaccess file.
DocumentRoot of VirtualHost is D:\dev\www\ for example.
ServerName is devwork.webdev for example.
HOSTS entry is 127.0.0.1 devwork.webdev.
VirtualHost file does have a default DocumentRoot being DocumentRoot "D:/xampp/htdocs". It works just fine.
The projects are each in a folder under D:\dev\www\ for example D:\dev\www\project01\ or D:\dev\www\project02\ and so on and show nicely in the browser when going to devwork.webdev with Options Indexes FollowSymLinks enabled. Apache is not showing any error and the access log file is also OK, things are working.
Now in my HTML when I use Project 01 a click on the link does link me to D:\dev\www\ showing all the projects I have in that folder.
Instead I would like to be linked to the root of the project, being D:\dev\www\project01\ or rather http://devwork.webdev/project01/.
How can I get that to work?
I am looking for a solution to this so that I can do dev work locally and without changing the HTML later FTP the data to the live host's root and it will work.
I have read and tried the following:
http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
https://perishablepress.com/redirect-subdirectory-to-root-via-htaccess/ .htaccess How to redirect root URL to subdirectory files, rewrite to clean URL AND not affect subdomains?
http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
https://stackoverflow.com/a/990405/1010918
How to redirect /directory/index.html and /directory/index.php to /directory/
Redirecting /directory/index.html to /directory/
with
How to remove .html from URL
and
http://forums.modx.com/thread/77211/endless-friendly-url-redirect-from-subdomain-folder-location
being closest to what I think I need but I must be doing something wrong since I always arrive at the DocumentRoot instead of the folder where the project is kept under the DocumentRoot.
Thank you for any help.
This seems closest:
Add VirtualHost definition for each project you want to access. (I'm
not sure how to do it on XAMPP for Windows).
For example, project01.devwork.webdev...
Set DocumentRoot for this VirtualHost to D:\dev\www\project01...
Add the hostname to your /etc/hosts/ file.
Open http://project01.devwork.webdev/ in your browser.
You should see the application in D:\dev\www\project01, while all URLs
will be based on "/".
What happens in the background:
When you open the URL http://project01.devwork.webdev/ in your browser, it will (as usual) translate it to IP address, but along with the request, it will also send Host header with the entered hostname:
GET / HTTP/1.1
Host: project01.devwork.webdev
Based on the Host field, Apache will decide which VirtualHost it needs to "pretend" to be, and serve files from the respective direcory.
However, if you need to have index of the projects, you will have to create it manually with full URLs.
An alternative with the given logic would be to simply add a VirtualHost for each project in D:dev\www\projectname by giving it its own domain instead of a subdomain on the host.
There is little difference there now when already editing httpd-vhosts.conf (using XAMP this file is in InstallDir and then in apache\conf\extra )but ideally it makes copying the local HTML to the live server without changing the HTML possible.
So this is something devs doing local work should keep in mind. I certainly will!! Thank you for your help and information.
<VirtualHost *:80>
DocumentRoot "D:/dev/www/newprojectname"
ServerName newdomainname.webdev
ServerAlias www.newdomainname.webdev
ErrorLog "D:/dev/www/log/dev-apache.error.log"
CustomLog "D:/dev/www/log/dev-apache.access.log" common
<Directory "D:/dev/www/newprojectname">
AllowOverride All
Options Indexes FollowSymLinks
Require local
# more detailed local
# Require ip 192.168.188
# or the IP from your local network
</Directory>
</VirtualHost>
I am struggling to get my head around an htaccess rule to redirect requests for an html file to go to a custom post. I have looked on here and in other places and nearly got there.
I want to redirect ONLY mydomain.com/profiles/.html to mydomain.com/name_profile/
So mydomain.com/profiles/smith.html to mydomain.com/name_profiles/smith. There are some 900 html files to be redirected and they are all contained in this directory. Other html files in the domain I need to leave correctly associated.
I currently have
RedirectMatch 301 ^/([^/]+)/([^/.]+)\.html$ /$1/$2/
RedirectMatch 301 ^/([^/]+)/([^/]+)/([^/.]+)\.html$ /$1/$2/$3/
But this redirects all html pages not just the ones in the profiles directory.
I am new at htaccess and have found several tutorials but none at a level I can understand, so any help is most welcome.
Place this rule just below RewriteEngine On line:
RewriteRule ^profiles/([^.]+)\.html$ /name_profiles/$1 [L,NC,R=301]
Use this rule:
RewriteEngine On
RewriteRule ^profiles/([^.]+)\.html/? name_profiles/$1 [DPI,L,R]
Or better, remove your RewriteMatch rules and replace with this:
RewriteEngine On
RewriteRule ^([^/]+)/([^/.]+)\.html/? $1/$2 [DPI,L,R]
RewriteRule ^([^/]+)/([^/]+)/([^/.]+)\.html/? $1/$2/$3 [DPI,L,R]
This assumes that mod_rewrite is both installed and activated for htaccess files.
If you are not sure, to check if mod_rewrite is installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All
My old website was built by handcoding HTML and has the directory structure:
public_html
.htaccess
index.html
...
content_folder
docs_folder
.htaccess
doc1.html
doc2.html
doc3.html
...
My new website is built using a CMS that stores its webpages in a database and so there is no traditional website directory structure like that shown above.
The public_html/docs_folder/.htaccess file above contains the following rules:
# Only permit access from links coming from my website (e.g.http://www.mydomain.com).
SetEnvIf Referer http://www.mydomain.com internal
<Files *>
order Deny,Allow
Deny from all
Allow from env=internal
</Files>
Since there's no directory structure in the new website, I wonder if there's anyway I can include the public_html/docs_folder/.htaccess rules above, which only apply to webpages in the docs_folder directory (and webpages in any sub-directories), inside the public_html/.htaccess file instead.
Is there a <location> attribute or other means to include a rule in an .htaccess file but have it apply to a sub-directory rather than the current directory the .htaccess file resides in?
Could someone give an example using the rule above?
Also, does <Files *> as used above cover all files (.html, .pdf, .css, etc.)? I'm hoping so.
Try adding this in the htaccess file in the root directory:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://yourdomain\.com [NC]
RewriteRule ^docs_folder/ - [L,F]
You'll need to remove the htaccess file in your /docs_folder/
I have a staging server (VPS) that has a dedicated IP address. For example, http://numeric.ip.address/
I can access the the files in the public_html folder by doing the usual tilda thing in the URL - http://numeric.ip.address/~account/
I have a folder I've developed locally that uses root-relative paths to resources, but
when I load the file in the browser, those root-relative URL's jump up in the directory all the way to the root IP address, instead of the public_html folder /~account/.
I realize there are better ways to set up a staging server and I plan to do so in the future, but I'm facing a deadline where it would be really handy to make this work.
I've tried tossing a base href tag in header tags, but that isn't doing the trick.
In Apache, normally you have "VirtualHost" container or .htaccess file. In it, you set your domain and DocumentRoot.
From what you describe, all you need to do is set your DocumenRoot correctly to you root of your site. then in the Browser, you can point to your paths. So if you have folder called "finance" and inside that folder you have document call "report1.pdf", you can put in your browser, example.com/finance/report1.pdf -as long as you have your DocumentRoot and site setup correctly and put the files in correct path, Apache will serve the file.
<VirtualHost 10.1.2.3>
ServerName example.com
ServerAlias www.example.com # not necessary
DocumentRoot /www/docs/host.foo.com
...omitted the <Direcotry> </Directory> stuff
</VirtualHost>
Is there a simple way to remove the .html from a URL namespace? For example, if I have www.mywebsite.com/special.html how can I make this www.mywebsite.com/special
Again, I'm using Apache on an Amazon EC2 instance. Thanks in advance!
Put this in your .htaccess file:
RewriteEngine On
RewriteRule ^(.+)\.html$ /$1
Make sure that mod_rewrite is installed and AllowOverride includes FileInfo.
Edit on how to get mod_rewrite working:
Usually, mod_rewrite already comes with apache, but sometimes needs to be enabled by the command
a2enmod rewrite
The AllowOverride directive sets permissions for .htaccess files. For mod_rewrite, FileInfo is required. To set this, change in your apache config file (usually /etc/httpd/conf/httpd.conf) the line
AllowOverride None
to
AllowOverride FileInfo
or
AllowOverride All
Make a directory named special and put an index.html inside of it with the markup from special.html.