how to pass subfolder params in htaccess - .htaccess

I am trying to pass the parameters of a subfolder of a url but I have not been successful. I only get the parameter from url 1 but when trying to get the parameter in url 2 it throws me access forbidden error
url 1 - web.com/folder/param1
url 2 - web.com/folder/subfolder/param1
RewriteEngine on
RewriteBase /folder
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]*)/?$ index.php?param1=$1 [L,QSA]

With your shown samples, please try following htaccess rules. Please make sure your htaccess is placed along side with folder folder(not inside it).
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine on
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
##2 parameter handling rules here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ index.php?param1=$1&param2=$2 [L,QSA]
##1 parameter handling rules here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ index.php?param1=$1 [L,QSA]

Related

Htaccess - manage catch all in multiple directories and one single htaccess file

I'm trying to dispatch all traffic in multiple directories based on a specific keyword.
I have the following directory structure:
dotcom/
dotcom/directory1/ (with subdirs)
dotcom/directory2/ (with subdirs)
dotcom/directory3/ (with subdirs)
I have a .htaccess file located in dotcom and I would like to redirect everything behind each directory to an index file in each directory.
Example:
dotcom/directory1/anything/blabla to dotcom/directory1/index.php
dotcom/directory2/anything/blabla to dotcom/directory2/index.php
dotcom/anythingNotExisting to dotcom/index.php
Anything not in one of the existing directories should be redirected to dotcom/index.php
I tried the following for dotcom:
RewriteEngine On
RewriteCond %{ENV:HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This catches everything
But when I tried to add conditions like the following, I get a 404:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/directory1/(.*)$ directory1/index.php?path=$1 [NC,L,QSA]
With this, if I try to access dotcom/directory1/blabla I have a 404 while if I access dotcom/directory1/ it goes to the right index.php
I have tried to use the full path dotcom/directory1/ but it doesn't help.
You may use these rules inside dotcom/.htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# ignore all rules below this for real files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# for URIs starting with know directory paths
RewriteRule ^(directory1|directory2)/(.*)$ $1/index.php?path=$2 [NC,L,QSA]
# everything else
RewriteRule .+ index.php?path=$0 [L,QSA]
I have found something that works with the following:
RewriteEngine ON
RewriteCond HTTPS off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^directory1/(.*)$ /dotcom/directory1/index.php?path=$1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^directory2/(.*)$ /dotcom/directory2/index.php?path=$1 [NC,L,QSA]
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /dotcom/index.php?path=$1 [NC,L,QSA]
This way I'm catching everything from /directoryX/ and redirect it to the root of the directory, everything else go to dotcom

404 errors after url rewrite with .htaccess

I'm using url rewriting in my website.
I've these url:
works
example.com/_new/app/planning
example.com/_new/app/user_info
doesn't work (404)
example.com/_new/try
example.com/_new/features
Here my rules:
RewriteEngine On
# Removing extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# example.com/app/[module]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)$ app.php?param1=$1 [L]
# example.com/app/[module]/[action]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)/([^/]+)$ app.php?param1=$1&param2=$2 [L]
# example.com/app/[module]/[action]/[type]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)/([^/]+)/([^/]+)$ app.php?param1=$1&param2=$2&param3=$3 [L]
# example.com/app/[module]/[action]/[type]/[id]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ app.php?param1=$1&param2=$2&param3=$3&param4=$4 [L]
Here the file structure:
/_new/
/_new/try.php
/_new/login.php
/_new/app.php
The try.php and index.php should be shown as example.com/try or example.com/login.
The app.php loads module from another directory. This one should be:
example.com/app/module_name.
What's the problem ?
Thanks.
In htaccess, the base URL example.com will be skipped in the matching, and in your file, the first regex ([^\.]+) will match /_new/try and it tries to redirect you to a file /_new/try.php which does not exist, hence 404.
Make sure to test your regex before trying to use the URL, you can benefit from https://regex101.com/ to make your tests.

.htaccess How to check if URI + file extension doesn't exists before RewriteRule

When a page is accessed via an URL like this:
/pagename
How can we check if the following file exists via htaccess
/pagename.htm
And then load index.php if not?
Here's what I'm working with:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I've tried a variation of this but no luck:
RewriteCond %{REQUEST_URI} !/.htm -f
# If the URI is not .htm and doesn't have any additonal params, try .htm
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?![^/]+\.htm)([^/]+)$ $1.htm [L,R=302]
# Adding the R flag causes the URI to be rewritten in the browser rather than just internally
# If the URI is .htm but it doesn't exist, pass to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/\.]+\.htm)$ $1/no [L]
# Passes requested path as 'q' to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/no/?$ index.php?q=$1 [L]
Should work rather elegantly for you. If it can't find the file, it just passes it on to index.php. You can of course modify what variable it uses, I just used 'q' for generic purposes.
Also, do note that you can condense this down to a single set of RewriteConds. I've expanded them for annotation purposes here.

htaccess physcially redirect in case of 404

To start off I know how to do a simple redirect to a 404 when a page doesn't exist with.
ErrorDocument 404 /index.php
Is what I want to do is a physical redirect to the index page. So if a user types in mydomain.com/abc and that page doesn't exist it does an actual redirect TO the index page. I don't want the url to remain the same with the bad address. Yes I know its not a big deal but it bugs me. I could accomplish this by having a custom 404 page redirect to the homepage but I don't want this.
I've tried this with the [R] redirect flag and obviously it doesn't work.
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^(.*)$ /index.php [L,R]
Update
This is the error I get it rewrites the url to mydomain.com/index.php
I tried to remove the index.php with just / and it didn't redirect but gave the same error below.
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept
cookies.
Do this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !(?:\.\w+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([^/]+)/?$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,R]
Explanation:
checks whether the file has extension:
RewriteCond %{REQUEST_URI} !(?:\.\w+)$ [NC]
If not, checks whether file is present:
RewriteCond %{REQUEST_FILENAME} !-f
If not a file, checks whether it is a folder which is present:
RewriteCond %{REQUEST_FILENAME} !-d
If not append .php. If / is present at the end, remove it and append .php
RewriteRule ([^/]+)/?$ $1.php
Checks whether the .php appended or whatever extension file is actually a file which is present:
RewriteCond %{REQUEST_FILENAME} !-f
check whether it is a directory:
RewriteCond %{REQUEST_FILENAME} !-d
If not, then it is a invalid file request and redirects it to /
RewriteRule ^(.*)$ / [L,R]
This should be your mod_rewrite based 404 handling:
# if it is not a file, directory or link
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php [L,R]
However I am not sure what are you doing in your first rule? Are you trying to add .php to each request URI?

htaccess is not ignoring images like it should?

I have an htaccess file that is supposed to be redirecting any non-existing files/folders/etc into the application's index.php script where they are handled by the application's SEO rewriting.
The problem is that any missing images are also redirecting into the application which is causing a lot of additional load and is unnecessary. I've been attempting to get this htaccess file to ignore images, but even though this should be working, it's not, and I'm completely out of ideas as to why...
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !.*\.(gif|png|jpe?g|ico|swf)$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?sef_rewrite=1 [L]
When I make a request to http://www.domain.com/folder_that_doesnt_exist/image.jpg this redirects to index.php
When I make a request to http://www.domain.com/folder_that_does_exist/image.jpg it also redirects to the index script
I'm not sure what I'm missing here because unless OR is specified, shouldn't the RewriteRule only be applied if the request passes all of the RewriteCond statements? which it clearly should not be passing...
Update:
I've modified the code to the following just to eliminate possible issues, but it still redirects all non-existing images into the script...
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(gif|png|jpe?g|ico|swf)$ [NC]
RewriteCond %{REQUEST_FILENAME} !\.(gif|png|jpe?g|ico|swf)$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?sef_rewrite=1 [L]
Maby try this:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} \.(gif|png|jpe?g|ico|swf)$ [NC]
RewriteRule .* - [R=404,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]

Resources