htaccess, a shortened url should not match a file - .htaccess

I have a little problem with my apache2 and .htaccess rules.
for example:
I have a shortened uri like
www.domain.tld/sitemap
which has to be rewritten by a rewriterule, redirected in a php File to display the sitemap.
The problem is, that in the root folder a file named sitemap.xml exists.
My apache automatically calls the sitemap.xml file but i don`t want that.
The file should be only called when uri is
www.domain.tld/sitemap.xml
is there a possibility to avoid the call of this file when the shortened URI is called?
this is just an example. There are some files that are required to be in the root folder and can`t moved from there into a subfolder (which would be the easiest way to fix this problem, but its not possible in my situation). it is required that these files are callable by uri.
Has anyone an idea how to fix this problem?
my current .htaccess file
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php?urlseg=%1&%{QUERY_STRING} [NC,L]
Thanks a lot!

You likely have MultiViews enabled, which auto-resolves your non-existent resource /sitemap to the existent resource /sitemap.xml. Especially in cases where you're using mod_rewrite, I really see no need for MultiViews, so you can turn it off by adding this to the top of your .htaccess file:
Options -MultiViews
Doing so should hopefully prevent this from happening.

Related

Mod_rewrite only working with certain folder names

Context
I'm using mod_rewrite to make my links better for SEO. I made the following rule for my page expanded_debate.php:
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^poll/([0-9a-zA-Z_-]+)/([0-9]+) expanded_debate.php?poll_title=$1&pollid=$2 [NC,QSA,L]
When I input this format in the URL (poll/filename/10, for example) I get a 404 error:
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.46 (Unix) OpenSSL/1.1.1h PHP/7.4.12 mod_perl/2.0.11 Perl/v5.32.0
However, when I change the first folder name to certain words, such as "debate" and "expanded_debate" (but not "expandedebate"), the file loads after page refresh. For example:
RewriteRule ^debate/([0-9a-zA-Z_-]+)/([0-9]+) expanded_debate.php?poll_title=$1&pollid=$2 [NC,QSA,L]
works fine.
I have an older .htaccess file, titled ".htaccess11", with the following info, in case it's of any use:
#forbids users from going to forbidden pages
IndexIgnore *
Options -Indexes
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
#404 error directions
ErrorDocument 404 /404.php
Question
Any idea why only certain terms in the first folder position ("^debate" in example above) work when using mod_rewrite?
There are no "poll" folders in my project, if that's of any interest.
Let me know if there are any questions.
The line
RewriteCond %{REQUEST_FILENAME}\.php -f
Means "Take the requested URL, map it to a full local path in the normal way, append .php to the resulting path, and then process the following rewrite rule only if there is an existing regular file at the modified path".
For example, the URL "poll/filename/10" will be rewritten only if there is a file called "poll/filename/10.php" in the relevant location.
Since the value of the AcceptPathInfo directive is evidently set to On, this condition will also be met if there is an existing file called "poll.php" or "poll/filename.php". That is why the rewrite rule works when you change "poll" to "debate" or "expanded_debate" – there are existing files called "debate.php" and "expanded_debate.php".
In any case, it sounds like this behavior is not what was intended. Removing the -f condition should give the desired result. Or, to prevent the rewrite rule from making existing files inaccessible, you could replace it with:
RewriteCond %{REQUEST_FILENAME} !-f
The exclamation point negates the -f test: "continue only if this file does not exist"
If you are using the %{REQUEST_FILENAME} server variable (anywhere), you should be aware of how the AcceptPathInfo directive will affect this, and consider setting that directive explicitly in the same .htaccess file.
If Options +MultiViews is in effect, then %{REQUEST_FILENAME} will match existing files whether or not the extension is included in the request (GET /foo will match an existing file "foo.php", "foo.html", etc.). And GET /foo.php will match in any case. So, omit the string "\.php" from the original rule.
Other configuration may also have an effect, too. The important point is that, unlike %{REQUEST_URI}, %{REQUEST_FILENAME} invokes all the processing that Apache would otherwise do to translate a URL into a local path.
(source)
NB: although I don't think it was the intention here, you actually might want to test for the existence of a local file as part of this rule. You could use a RewriteCond to check whether the back-end data file for a given poll has been manually created, and return 404 by default if it has not. That would be a simple way to prevent users from making up their own poll URLs at will.

removed .html extensions with htaccess now index.html give 403 error

After entering the code below, my home page gives a 403 error. The rest of the site works perfectly. All instances of .html were removed.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Any advice?
Thank you!
example.com leads ti the 403 error. If I write example.com/index it works fine.
Something else must have changed for this to result in a 403 error. The code you posted won't actually do anything when you request example.com/ - the same as if that code didn't exist at all. (UPDATE: However, this assumes your .htaccess file is located in the document - it appears this is not the case - see below.)
However, what will trigger a 403 in such cases is when "formatted directory listings" are disabled and the directory index document cannot be found (or has been disabled).
So, try setting the appropriate directory index at the top of your .htaccess file:
DirectoryIndex index.html
It is the DirectoryIndex that serves the appropriate file when requesting your "home page", not your directives in .htaccess.
UPDATE:
It [.htaccess] is located in my root directory. Would it be better to put it in the public_html folder?
Yes, the code you posted should go in the /public_html directory (ie. your document root). If these directives are in a .htaccess file above the document root then the RewriteRule pattern will match the URL-path public_html/ and rewrite the URL to public_html/.html which is possibly where your 403 error is coming from ("dot" files are usually hidden/protected OS files and you may also have a directive in your server config blocking access. However, this behaviour may also be dependent on other factors in the server config/OS). However, with that code in the document root then a request for example.com/ (your home page) won't be processed by these directives (which is good) - mod_dir should then serve the index.html file in this instance.
However, you don't want to process "directories" anyway (public_html is obviously a "directory", not a file). Which is what's happening above. eg. .html shouldn't be appended to public_html/ to begin with (or example.com/path/to/directory/ or any other directory). This can be avoided by adding an additional condition to your rule block to avoid directories (as well as files). For example:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.html [L]
Simply adding that additional RewriteCond directive might be enough and still allow you to keep your .htaccess file above the document root. (However, you may still need to move the .htaccess file as well, as described above.)
Also, the NC flag is not required here and literal dots don't need to be escaped when used inside a character class.
You could also extend this code to first check the existence of the file (with a .html extension) before rewriting, although this may be unnecessary in your case. For example:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^.]+)$ $1.html [L]
This requires an additional "file check" which may be an unnecessary overhead.

htaccess query. 2 rewrites rules for 2 home pages

I'm hoping someone might be able to help with a problem I'm having due to my lack of experience and knowledge with htaccess.
What we're doing is running IP Boards forum software and wordpress both in the root directory. The IPB has the index.php file (because of having indexed url's) and the new Wordpress's index.php file has been renamed to blog.php.
At the very top of the htaccess file we've added: DirectoryIndex blog.php index.php - so the new wordpress opens first.
The problem I'm having is trying to have 2 rewriterules in the htaccess file for the friendly urls from the forum software and also the permalinks for the new wordpress.
I can only seem to have one or the other.
Please could anyone tell me, or point me in the right direction to get both working.
This is what I'm doing so far but sadly no joy, but works fine if we remove one of the condition and rewrites.
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog.php [L]
</IfModule>
Many thanks in advance.
Not sure if you still want an answer. Either way, if you are trying to go to two different pages you need some way of distinguishing them.
DirectoryIndex basically tells the default file (and order) when entering a directory. So http://host.com/ with both blog.php and index.php in the directory will serve up blog.php because it is first in the list you gave the server. If there is only index.php, it will serve that. If neither (and you don't have anything else in the list) it will throw a 404 because no default file is found.
EDIT: it will try to list contents if not found. My bad. If you don't allow directory listing, then it will probably show an error code. To turn off directory listing look in options: http://httpd.apache.org/docs/current/mod/core.html#options
http://httpd.apache.org/docs/current/mod/mod_dir.html
Your rewrite rules seem to kind of want do the same thing in a different order. If you request http://host.com/a and a is not a file or directory (according to the conditions) it will go to index.php.. if index.php doesn't exist, then it will loop until the server catches it, because you don't check that. So, that means the second set of conditions don't do anything, because either index.php exists or it doesn't and the next set probably won't really be reached unless it does.
You need to decide how to differentiate the two (/blog/ for the blog.php and / for index.php or something) and make one of them the default. If you want to randomize it, I would suggest doing that through PHP.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
.. to redirect from root to /forums/ through htaccess try this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/forums/
RewriteRule ^(.*)$ /forums/$1 [L]

Change Displayed URL Structure using mod_rewrite NOT Working

I need to change the structure of the displayed client-side URL. I'm not too skilled using regex and coding for the .htaccess file. Basically, I have a structure that looks something like:
http://www.example.com/catalog/index.php?cat=lt&sec=lt1-1&id=nmlt10.
I would like this to be displayed in the address bar as:
http://www.example.com/catalog/lt/lt1-1/nmlt10.
This is what I came up with, but it has had no effect:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\$ /catalog/index.php?cat=$1&sec=$2&id=$3 [L]
I tested and removed any other rules in the .htaccess file to ensure nothing was being overwritten. I'm on a shared hosting apache server, and know that mod_rewrite is enabled, because I use it to rewrite non-www to www urls. I don't receive and 500 error messages, I just do not notice any change at all. I'm not sure where I'm going wrong here, so hopefully someone can point me in the right direction.
Finally found a solution that worked:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
Appreciate LazyOne's response to get me on the right track; however, when using:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I wasn't able to following links that were already placed on the site, it treated different directories as the variables, for example, when browsing to an image or file, say:
folder/folder/image.png
It would grab "folder" - "folder" - and "image" as the variables. I can see why that was happening, if anyone has a different solution or an explanation, please let me know, I'm always willing to learn.
Since your .htaccess is in website root folder, then you should use thus rule:
RewriteEngine On
RewriteBase /
RewriteRule ^catalog/([^/]+)/([^/]+)/([^/]+)$ /catalog/index.php?cat=$1&sec=$2&id=$3 [QSA,L]
If you place it in .htaccess in /catalog/ folder, then you can remove catalog from it:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I have tested rule before posting -- works fine for me.
This rule (same as above) will check if URL is a file or folder and will only rewrite if it is not:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]

How to avoid URL and directory match problem when editing .htaccess file?

I have the following lines in the .htaccess file in the site directory:
RewriteEngine On
RewriteRule ^([a-z]{1}[a-z0-9_-]{3,20})$ account.php?username=$1&%{QUERY_STRING}
If it receives URL for example :
http://localhost/samplesite/johnsmith
it will rewrite it to
http://localhost/samplesite/account.php?username=johnsmith
which is fine.
The problem occurs when there is a directory named johnsmith in the site directory. then the URL is rewritten to and displayed as
http://localhost/samplesite/johnsmith/?username=johnsmith
and that is a problem. I am trying to implement account pages functionality for every user but if a user wants to register a username like some directory in the root the functionality will break? I tried adding rewrite conditions to check if the requested URL stands for an existing directory or a file :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
but I don't know how to proceed.
If someone knows a better way to do account pages functionality for users I would appreciate to give me a piece of advice on that.
Can anybody help me solve this case? Thank you!
Your RewriteCond will work if it is in the correct place. Your full .htaccess should look like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# ---------------------------------------------------------------See info below--
RewriteRule ^([a-z]{1}[a-z0-9_-]{3,20})$ account.php?username=$1&%{QUERY_STRING}
Also, you don't need the %{QUERY_STRING}. Instead you should use the QSA flag to append the rest of the query string...
RewriteRule ^([a-z]{1}[a-z0-9_-]{3,20})$ account.php?username=$1 [L,QSA]

Resources