error 404 defining URL in mod rewrite - apache

I'm trying to rewrite my URL's to be more clean and user friendly and also better for SEO, so whenever the user clicks each country link to see the list of train journeys for each country, i.e: Italy, it should call the page country.php?country=italy , but the URL should be rewritten to great_train_journeys/country/italy.
I've tried to set rewrite rules on a .htaccess file but i'm getting the 404 error.
Here is my code for the .htaccess file:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^country=(.*)$
RewriteRule ^country/([A-Za-z0-9-]+)/?$ country.php?country=$1 [NC,R]
I'm using XAMPP to work on my local server, so my project folder is inside the HTDOCS folder, which is the root of my server:
Here is my project structure:
I've checked if mod_rewrite is enabled in the http.config file and also changed the AllowOverride to all like it is below:
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Thanks for your help

I'm trying to rewrite my URL's from this, country.php?country=Italy to this country/Italy
I believe you meant it the other way around, at least this is what your Code tells me. So if someone enter example.com/country/italy you want that the internally the this /country.php?country=italy is called but the user should not see it.
So in this case you need:
RewriteEngine on
RewriteRule ^/?country/([^/]+)/?$ /country.php?country=$1 [NC,L]
But if you mean it the other way around so that the URL in the browser is example.com/country.php?country=Italy but this should internally go to example.com/country/Italy than you need the following:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*&)?country=([^&]*)(&.*)?$
RewriteRule ^/country\.php$ /country/%2 [NC,L]
With this we have some small problems because if example.com/country/Italy is the real folder and someone enter example.com/country.php?country=italy that we will not find the folder with the name italy and we get a 404 error.
In your original Code you also used the [R] Flag (that means redirect), for exapmle if a user enters example.com/country.php?country=italy that the URL in the browser will change to example.com/country/italy
than you should do this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*&)?country=([^&]*)(&.*)?$
RewriteRule ^/country\.php?$ /country/%2 [NC,R=301]
Now we do a 301 redirect.

Related

How does .htaccess behave when located in directory above doc root

I have the following rules in my .htacess file. They work perfectly fine as is, but I have several doc roots which I would like to apply the same rules to, all in the same parent directory. Is this possible with .htaccess?
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/some/path($|/)
RewriteRule (.*) /some/path/$1 [L]
FallbackResource /some/path/index.html
I know .htaccess is somewhat relative to the directory they're in, so I'm thinking I need to modify these somehow.
The end result from just moving it directly to the parent directory is everything returns the contents of /some/path/index.html
For anyone this helps, I accomplished this by adding it to the "post virtual hosts" section of my main apache config.
<Directory "/home/*/parentdirectory/*">
RewriteEngine on
RewriteRule !/some/path/ /some/path%{REQUEST_URI} [L]
FallbackResource /some/path/index.html
</Directory>

.htaccess - Deny from all except index.php and current directory ./

.htaccess
'Deny from all' all files except index.php and current directory ./
Hi all, I'm trying for too much time a stupid thing that had to work but i don't know how to do it.
In this way i can give 403 error to users for all files except index.php page (images and CSS also)
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
<Files /index.php>
Order Allow,Deny
Allow from all
</Files>
But I want to add also the current directory [./] to allowed search
How to do it? Plz 🔥
Because if I search THIS URL from my browser --> https://hostname.com/403dir/ it gives me 403 error and I don't want it.
TNKS
With your shown samples, could you please try following. Make sure your htaccess file is in your root path(besides 403dir path NOT inside 403dir).
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_URI} !^/403dir/?$ [NC]
RewriteCond %{REQUEST_URI} !index\.php$ [NC]
RewriteRule ^ - [F,L]
OR use a single condition itself as follows, make sure either you use above solution OR following solution only at a time please.
RewriteEngine ON
RewriteCond %{REQUEST_URI} !(^/403dir|index\.php$) [NC]
RewriteRule ^ - [F,L]

List directory contents with Apache

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!

Apache Mod Rewrite -- fake a folder when infact there is one

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]

apache redirecting with 301, instead of internal rewrite

I'm trying to map/mask a subfolder to a different folder on the same server. I've done it plenty of times before but I can't get this to work properly. I want the user who accesses directory "a" to see "oldsite/a" instead, but I do NOT want them to be redirected, or to see the new address in the browser.
<IfModule mod_rewrite.c>
RewriteEngine on
Options +FollowSymlinks
RewriteRule ^\.htaccess$ - [F]
# Maintain support for old structure
RewriteRule ^a/([0-9]+)$ oldsite/a/$1?redirected=1 [QSA,L]
</IfModule>
I've tried various combinations with RewriteBase, etc. Instead of the user seeing "domain.com/a/1234" they are redirected to "domain.com/oldsite/a/1234?redirected=1".
Turns out the statement was missing a closing forward-slash before the query string.
RewriteRule ^a/([0-9]+)$ oldsite/a/$1/?redirected=1 [QSA,L]