Here is my .htaccess:
<Files.htaccess>
order allow,deny
</Files>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(\w+)$ ./redirect.php?url_token=$1
My script's logic:
My script is not in the root of the website, it's in a folder (ex: www.mywebsite.com/script/). I have the .htaccess file in this folder.
The PHP script gets that url_token value and processes it, it is working well, i've tested it.
But when I use this .htaccess file and I try to access an URL like www.mywebsite.com/script/fa34d where fa34d is a random generated code, I get:
Error 500 on XAMPP and Access Forbidden on another online host.
What can be the problem?
I've already spent more time with this than the rest of the script.
You're likely creating a loop or you're redirecting to a file outside of your document root. If the folder accessed as the root is that same as what displays as www.mywebsite.com then you can set RewriteBase / in your access file. Remember all .htaccess files work together and also work with your VirtualHosts directives on the server.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)$ /redirect.php?url_token=$1
You just need to add this line to hide your directory from http request in your .htaccess file
Options -Indexes
Enjoy!!
Related
Let me start by saying that my knowledge of Apache is almost none, so I apologize if I am not using the correct terminology.
I have a website written in Vue, and the routing is taken care by Vue Router. In their documentation, they specify that in order for the router to work correctly, you have to put this in the .htaccess file of your website:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
From what I have been able to understand, all requests are sent back to the index.html file which will take care of loading the correct component based on the path.
My goal is to now allow my website to have a path (let's say /documents) which is not picked up by Vue, but instead shows the contents of the directory and allows you to both navigate and download the contents (Like this).
I have tried a few ways, but they all return a 403 or 500 (possibly due to a mistake in my config). I understand that I need to add a RewriteRule but all of those that I tried return weird errors.
Thanks in advance
You can have multiple rewrite rules based on what the RewriteBase is . In your current set, the rule is applying to the root of the host.
You can add another rule with RewriteBase /documents/. More info: What does RewriteBase do and how to use it?
I recommend reading the docs: https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
The RewriteCond directive defines a rule condition.
So here a dirty explanation:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
So your RewriteConds says that if the given path/url isn't a file (!-f) and not a directory (!-d) then the next rewrite rule (RewriteRule . /index.html [L]) takes action.
RewriteRule . /index.html [L]
"." is a wildcard, so all urls will be redirect to index.html.
The [L] flags stops the execution (https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_l)
The RewriteRule ^index\.html$ - [L] stops the execution if the url is index.html.
So, your rewrite rule fulfill your requirements and seems correct.
When you get a 403 you maybe need to add Options +Indexes to your config or htaccess.
In the end, after looking through the docs, I was not able to understand how to set it up. I found this page, and using option #2 I was able to get the directory to at least show up.
I then added the auth to the folder through the .htaccess file and added the .htpasswd file with the username/password combo
TLDR
Create the folder in the location you want. In my case it was in httpdocs/documents
Create a .htaccess file where you put the following contents:
# Omit this section if you do not need the auth
AuthType Basic
AuthName "restricted area"
AuthUserFile /path/to/your/.htpasswd
require valid-user
Order allow,deny
Allow from all
Options +Indexes
Create the .htpasswd file in the location you specified above. To generate the username/password combo I used this
Any corrections are welcome!
In my Apache server, I have the following structure:
/www
.htaccess
api.php
index.php
My .htaccess is a very simple file that redirects all the traffic to index.php:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?path=$1 [L,QSA]
And my index.php file just prints whatever the path is:
<?php print_r($_REQUEST['path']); ?>
So when I try to access to web.com/test, I get a web that outputs Array ( [path] => test ).
However, when I try to access web.com/index or web.com/api, it returns a 404 error. I have checked that this only happens when I try to access a route with the name of an existing file. How can I prevent this behaviour?
Turns out that I have MultiViews enabled in apache2.conf file. From the Apache docs:
The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name.
So I just had to add Options -MultiViews either to my apache2.conf (server-wise) or to the .htaccess of my project (project-wise) to prevent that behaviour.
What I'm trying to do is to enable a 'dry' version of url rewriting in Apache only to check if it is working and actually do not rewrite anything yet.
The simplest .htaccess that I came up with is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
But this still doesn't work. All files, all paths I'm trying to request, no matter if they exist or not return 403 Forbidden
What I need is any example htaccess that has redirection inside that must work, that is allow access to all existing files and folders and do it's job only when the file requested doesn't exit.
The problem was that I've not added these lines:
Options +SymLinksIfOwnerMatch
or
Options +FollowSymlinks
I have, let's say, www.website.org/folder/ which inside has the following .htaccess
RewriteEngine On
RewriteRule ^folder/[0-9]+ http://www.website.org/folder/index.php?n=$1 [NC]
inside folder I have many folders like 1234, 4567, etc. The behavior I'm looking for is a rewriting from www.website.org/folder/1234 to www.website.org/folder/index.php?n=1234. However, for some reason the rewriting doesn't occur and I get a Forbidden error (given that you can't access the directory itself).
How can I solve this?
Thank you very much
-- Note: I had to put away Options +FollowSymlinks because I was getting a Option FollowSymLinks not allowed here error from the provider's webserver.
-- Edit 1
Following Jason's post I modified the .htaccess as follows (I still kept it in folder):
RewriteEngine On
RewriteBase /folder/
RewriteRule ^folder/([0-9]+)/?$ /folder/index.php?n=$1 [NC,L]
But it still brings me to the folder, why is this? Thanks!
The way your rule is written, .htaccess should be in your webroot not the folder directory.
Alternatively, you could modify your RewriteBase. However, I'd do the above and use:
RewriteEngine On
RewriteBase /
RewriteRule ^folder/([0-9]+)/?$ /folder/index.php?n=$1 [NC,L]
I have the rewrite ruls in .htaccess file under /var/www in my ubuntu10.4 server like below, When the url appears to be http://127.0.0.1/doc/view, the webpage shows "The requested URL /doc/view was not found on this server", and then I check the apach log, it logs "File does not exist: /usr/share/doc/view", so apprently apache redirect the /doc/view to /usr/share/doc/view. I don't know what it is going on here, can anybody help? thanks for help.
Options +FollowSymLinks
IndexIgnore */*
<IfModule mod_rewrite.c>
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
</IfModule>
Apache's default config on many distributions makes it so you can go to http://localhost/doc/to read the Apache documentation. You can edit this out in your httpd.conf or apache2.conf file if you need to use /doc for your own URLs.