Basically, I've got the following .htaccess file:
Options -Indexes
ErrorDocument 400 /index.php?error=400
ErrorDocument 401 /index.php?error=400
ErrorDocument 403 /index.php?error=400
ErrorDocument 404 /index.php?error=400
ErrorDocument 500 /index.php?error=400
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Enable sites without extentions
options +MultiViews
<IfModule mod_rewrite.c>
Options +FollowSymlinks
# Adaptive-Images -----------------------------------------------------------------------------------
# Add any directories you wish to omit from the Adaptive-Images process on a new line, as follows:
# RewriteCond %{REQUEST_URI} !ignore-this-directory
# RewriteCond %{REQUEST_URI} !and-ignore-this-directory-too
#RewriteCond %{REQUEST_URI} !img
# don't apply the AI behaviour to images inside AI's cache folder:
#RewriteCond %{REQUEST_URI} !ai-cache
# Send any GIF, JPG, or PNG request that IS NOT stored inside one of the above directories
# to adaptive-images.php so we can select appropriately sized versions
# RewriteRule \.(?:jpe?g|gif|png)$ adaptive-images.php
# END Adaptive-Images -------------------------------------------------------------------------------
# SEO Friendly Redirect Rules -----------------------------------------------------------------------
RewriteRule ^(admin)/?$ admin/ [L,NC]
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?title=$1 [L]
RewriteRule ^([A-Za-z0-9_]+)/([A-Za-z0-9_]*)/?$ index.php?title=$1&ch=$2 [L]
RewriteRule /$ index.php [END]
As of right now, the rules and conditions all work great except for one thing. If I navigate to say: "example.com/nonexistingfolder" , it is using this rule to process:
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?title=$1 [L]
Which brings up a php error because the argument passed is invalid. Which is to be expected, because it doesn't exist. But if I no navigate to: "example.com/nonexistingfolder/index.php" , I'm correctly greeted with my 404 page defined at the top. (don't worry about the slashes, in the original these are permalinks)
ErrorDocument 404 /index.php?error=400
I don't know too much outside of what I've learned today about .htaccess and mod_rewrite. Is there some other way I should be going about this? Or am I missing other statements?
I've tried adding the following lines to my .htaccess with no progress:
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
Either these don't help in my case (and I thought the second would for sure), or I am still doing something wrong.
I expected the code to be rather straightforward, however this has left me scratching my head for a couple of hours. Thanks for taking the time to read and ponder this with me! Have a good one.
EDIT: I added these three lines of code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /index.php?error=400 [L]
Which works when I navigate to something that doesn't exist. Yet, it interrupts the other RewriteRule I have set: RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?title=$1 [L]
So now I am for sure at a loss at what to do.
To further exemplify:
Say I navigate to "website.com/nonexistingfolder". Which now because I added the three lines above, shows the 404 page correctly. However, because of the single RewriteRule above, if I navigate to: "website.com/indexquery" it will also show the 404. How can I tell my .htaccess to differentiate between the two?
Related
i am new to .htaccess usage and tried to learn through online resources but however i write it the rules negate each other and am having a hard time writing a good enough .htaccess file below is my current .htaccess file which works fine for some pages like removing extensions and rewriting subdomains please check below
## Flag for GoDaddy
Options +MultiViews
RewriteBase /
## Remove extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !=f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
## Redirect from extensions to non-extensions
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
## Redirect Pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^post/([a-zA-Z0-9-/]+)$ /post.php?ps=$1
RewriteRule ^([a-zA-Z0-9-/]+)$ post-files.php?ps=$1 [L,QSA]
## Server Only
## Redirect from www - non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://$1/$1 [R=301,L]
## SSL Redirect
## RewriteEngine On
## RewriteCond %{HTTPS} ≠On
## RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
## Create Error Pages
ErrorDocument 404 /errors/404.html
ErrorDocument 403 /errors/403.html
ErrorDocument 500 /errors/500.html
## Redirect non-existing pages to index.php
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Above is the .htaccess am currently using and i got it through tutorials from youtube it works good and redirects files.php to files only which am happy with but
as you can see above this line RewriteRule ^post/([a-zA-Z0-9-/]+)$ /post.php?ps=$1 and the line below it are not passing through the ps but they show 404 page
i want the results to be domain.com/post-file-slug to go to exactly file domain.com/post-file.php?ps=post-slug-here for rule RewriteRule ^([a-zA-Z0-9-/]+)$ post-files.php?ps=$1 [L,QSA]
and
domain.com/post/post-slug-here to go to exactly domain.com/post.php?ps=post-slug-here for rule
RewriteRule ^post/([a-zA-Z0-9-/]+)$ /post.php?ps=$1
I was working on this for 2 days now hopefully fix it soon. Thanks
## Flag for GoDaddy
Options +MultiViews
In what way is this a "flag for GoDaddy"? Enabling MultiViews will cause the ps URL parameter not to be passed to the post.php script. You need to ensure that MultiViews is disabled for the later rewrites to work as intended. ie.
Options -MultiViews
MultiViews (part of mod_negotiation) essentially enables extensionless URLs. It will result in a request for /post/post-slug-here to be "rewritten" to /post.php/post-slug-here before your mod_rewrite directive is processed, so it never matches and never rewrites the request to include the ps URL parameter.
## Remove extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !=f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
## Redirect from extensions to non-extensions
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
It is currently MultiViews that is allowing your extensionless URLs to work. The first condition (RewriteCond directive) above is incorrect. It should be !-f (not a file), not !=f (does not equal "f" - always true). However, this is still "wrong", as you need to check that the .php file exists before rewriting the request. If you simply rewrite all requests that do not map to a file (which is what you are trying to do here) then the later rewrites to post.php, post-files.php and index.php will not be processed as intended.
The regex \s/+(.+?)\.php[\s?] in the second condition is not strictly correct as it will result in a malformed redirect if .php occurs in the query string when it is omitted in the URL-path. eg. A request for /foo?bar.php would result in a redirect to /foo?bar when there should be no redirect at all in this instance. The regex needs to capture the URL-path only, so change the subpattern (.+?) to ([^?]+) instead.
These two rules are also the wrong way round. The external redirect should be first. As a general rule, external redirects should always go before internal rewrites.
It should be like this instead:
## Remove extensions
## Redirect to remove ".php" extension
RewriteCond %{THE_REQUEST} \s/+([^?]+?)\.php[\s?] [NC]
RewriteRule \.php$ /%1 [R=301,NE,L]
# Rewrite to append ".php" extension if corresponding ".php" file exists
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)$ $1.php [L]
You should already be linking to the file without the .php extension. The redirect to remove the .php extension is for SEO only when changing an existing URL structure.
No need to backslash-escape a literal dot when used inside a regex character class. The NC flag was superfluous here.
## Redirect Pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^post/([a-zA-Z0-9-/]+)$ /post.php?ps=$1
RewriteRule ^([a-zA-Z0-9-/]+)$ post-files.php?ps=$1 [L,QSA]
RewriteCond directives only apply to the first RewriteRule directive that follows. So, the second rule above is processed unconditionally - is that the intention?
In fact, those two condition are probably superfluous. The regex would already appear to exclude actual files since the regex excludes dots. And do you need to be able to access filesystem directories directly?
The character class [a-zA-Z0-9-/] is "confusing". The last hyphen is seen as a literal hyphen (which is presumably the intention), but at first glance it can look like a range specifier (as used earlier in the character class). To avoid confusion when matching a literal hyphen inside a character class, either backslash-escape it, or move it to the first or last character in the character class. eg. [a-zA-Z0-9/-].
You are also missing the L flag from the first rule. (You've included it on the second.) Do you also need the QSA flag? (ie. Are you expecting additional URL parameters on the initial request?)
Having revised the "extension removal" rules above, this does not matter so much, but these rules that rewrite the request to post.php and post-files.php should really be above the "extension removal" rules.
## Redirect from www - non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://$1/$1 [R=301,L]
This rule is incorrect and in the wrong place. Canonical redirects (www to non-www and HTTP to HTTPS) should generally be above other rules. As mentioned above, redirects before rewrites.
But this rule is also wholly incorrect. $1 is a backreference to the first captured subpattern in the RewriteRule, so http://$1/$1 will naturally result in a malformed redirect. The first backreference should be %1 (to the last matched CondPattern) to match the requested hostname. Ordinarily, you should also be redirecting to HTTPS here, not HTTP. For example, the rule should read:
:
RewriteRule (.*) https://%1/$1 [R=301,L]
The ^ and $ surrounding the RewriteRule pattern are superfluous since regex is greedy by default.
## SSL Redirect
## RewriteEngine On
## RewriteCond %{HTTPS} ≠On
## RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Although commented out, it is also incorrect. It needs to go before the other rewrites. It should go at the top of the file if implementing HSTS or after the www to non-www redirect if not (and minimising the number of redirects).
The CondPattern in the preceding condition should be !on, not ≠On (which is wholly invalid on two counts... ≠ is not valid and the comparison is case-sensitive. HTTPS will always be lowercase.)
You are also missing the R=301 and L flags.
No need for a capturing group in the RewriteRule pattern, since this is not being used in the substitution string. ^ would suffice (and be more efficient) instead of (.*).
## Create Error Pages
ErrorDocument 404 /errors/404.html
ErrorDocument 403 /errors/403.html
ErrorDocument 500 /errors/500.html
For readability, you should define your custom error documents at the top of the file. (Technically, it doesn't matter.)
## Redirect non-existing pages to index.php
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
For readability you should define the Options together at the top of the file (with -MultiViews). For example:
Options -MultiViews -Indexex +SymLinksIfOwnerMatch
(Disabling Indexes - auto-generated directory listings - is a good idea.)
You do not need to repeat the RewriteEngine directive. (Only the last instance of this directive does anything.) It is logical to place this rule near the top of the file, before your first mod_rewrite directive. (Although technically, the position of this directive in the file does not actually matter.)
Aside: You should be consistent in the prefix you use on your internal rewrites. On some rules you include the slash prefix (eg. /post.php), and on some you have omitted it (post-files.php). You have defined RewriteBase / (which isn't strictly required here as it happens) - RewriteBase only applies to relative substitution strings (ie. when the slash prefix is omitted).
UPDATE:
also i have file i want to exclude like 404.php in root directory from how do i exclude somefiles from the redirect. when i sent ajax to backend php file it redirected to homepage and failed to retrieve data.
To exclude specific files you would add a rule like the following, after the canonical redirects:
# Exclude "/404.php" from stripping the ".php" extension
RewriteRule ^404\.php$ - [L]
Generally, once you go extensionless for .php files you should be extensionless everywhere. So, there should be no unexpected redirects. The redirect is really only for SEO.
With regards to your AJAX requests, if you are making POST requests, then you could simply exclude all POST requests from further processing. For example:
# Prevent further processing of POST requests to ".php" files
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule \.php$ - [L]
Alternatively (or as well as), if your AJAX requests are setting a custom HTTP request header then you can check for this as well.
Summary
Bringing the above points together, it should look like this:
## Disable MultiViews and Indexes
Options -MultiViews -Indexes +SymLinksIfOwnerMatch
## Create Error Pages
ErrorDocument 404 /errors/404.html
ErrorDocument 403 /errors/403.html
ErrorDocument 500 /errors/500.html
RewriteEngine On
RewriteBase /
#### Canonical redirects
## SSL Redirect
## RewriteCond %{HTTPS} !on
## RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
## Redirect from www - non-www
## >>> CHANGE TO "HTTPS://"
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule (.*) http://%1/$1 [R=301,L]
#### Rewrite Pages
RewriteRule ^post/([a-zA-Z0-9/-]+)$ post.php?ps=$1 [QSA,L]
RewriteRule ^([a-zA-Z0-9/-]+)$ post-files.php?ps=$1 [QSA,L]
#### Exceptions
## Exclude "/404.php" from stripping the ".php" extension
RewriteRule ^404\.php$ - [L]
## Prevent further processing of POST requests to ".php" files
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule \.php$ - [L]
#### Remove extensions
## Redirect to remove ".php" extension
RewriteCond %{THE_REQUEST} \s/+([^?]+)\.php[\s?] [NC]
RewriteRule \.php$ /%1 [R=301,NE,L]
## Rewrite to append ".php" extension if corresponding ".php" file exists
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)$ $1.php [L]
## Redirect non-existing pages to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
I have a site with custom .htaccess file that handles few things:
1) It treats urls without ".php" extensions as if it has ".php" in the end.
2) It redirects http:// and http://www. urls to https://www.
Here is the .htaccess file:
ErrorDocument 404 /404.php
Options -MultiViews
RewriteEngine On
## add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
# if not a directory and .php file is present then add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Everything works as expected, but I have observed some strange behavior that resulted in 500 errors instead of 404:
1) When you visit non-existed root level url such as https://www.example.com/skjdfhkj it redirects to 404 as expected.
2) When you visit non-existed nested url such as https://www.example.com/some-text/skjdfhkj where some-text does not match any existing php files, then it returns 404 as expected.
3) However, when you visit some non-existed nested url such as https://www.example.com/some-existing-page-name/skjdfhkj , where some-existing-page-name matches the name of existing php file (https://www.example.com/some-existing-page-name.php), then it gives out a 500 Server Error.
My question is: how do I update my htaccess to properly return a 404 instead of 500 when someone visits non-existing nested url such as https://www.example.com/some-existing-page-name/skjdfhkj (where some-existing-page-name matches the name of existing php file (https://www.example.com/some-existing-page-name.php)) ?
I guess it has something to do with mod rewrite that treats urls without .php extensions as if it had .php, but don't know how to modify htaccess to make it work properly :(
Try changing last rule as this:
# if not a directory and .php file is present then add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
%{REQUEST_FILENAME} sometimes can give unexpected matches from filesystem using partial matches.
I will provide a little background of my problem. I'm working on a static HTML5/CSS3 prototype for a PHP CMS. One of the things I want to is to have clean URLs. I had stuff like this at the start:
localhost:8888/file.php
And wanted it to:
localhost:8888/file or without the trail slash localhost:8888/file
I did a quick search on Stackoverflow and found several results over the matter and I kindly used this one to do the job:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*?)/?$ $1.php
RewriteRule ^(.*?)/?$ $1.php
The problem with this approach is when i want to set deep levels on my CMS structure, like site/posts. Without mod_rewrite I need to create folders and create intense changes over the code to make things work like ../images/ to ../../images/ and so on, so boo to that solution. I need to go with mod_rewrite to make it work.
In addition, that mod_rewrite chunk of code doesn't work either with the folder stuff so I started a search in here to find a solution so I can do /level1_level2_levelx.php to /level1/level2/level3/ or /level1/level2/level3. I did search in many ways but i don't find the correct answer to this.
Thank you all,
PD: I'm using MAMP.
On the Rewrite front I'm guessing the the following should do what you ask:
Internally rewrite entered URL:
http://localhost:8888/file
OR:
http://localhost:8888/file/
to:
http://localhost:8888/file.php
Rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteCond %{REQUEST_FILENAME} ^(.*)/?$
RewriteCond %1\.php -f
RewriteRule ^(.*)/?$ $1.php [L]
But sound like you really want a custom 404 handler configured in Apache e.g.
<FilesMatch "\.(css|js|jpg|png|gif)$">
ErrorDocument 404 "File not found."
</FilesMatch>
<Files favicon.ico>
ErrorDocument 404 "-"
</Files>
ErrorDocument 404 /404-handler-script.php
And an associated 404-handler-script.php added to do whatever mapping you want internally e.g.
http://www.phpriot.com/articles/search-engine-urls/4
Note: This script redirects the browser to the destinations script rather than serve it itself.
Right now I have a few folders (img, css, js, and ico) in the root directory of my website. However, I want to move them into a new directory called public_html. For example, instead of the image folder being located in /img, it would be instead located in /public_html/img. But I'm having problems doing this, and i suspect it's a problem with my .htaccess file.
Options -Indexes
Options +FollowSymLinks
RewriteEngine On
ErrorDocument 404 static/404.html
ErrorDocument 403 static/403.html
RewriteCond %{REQUEST_FILENAME} !-f
# ---------- Custom Routes ----------------
# -----------------------------------------
RewriteRule ^(js|css|img|ico)\/(.+)$ public_html/$1/$2
RewriteRule ^(.*)$ index.php?r=$1 [L,QSA]
I can access /public_html/css/style.css just fine, but when I add in that first RewriteRule line and try to access /css/style.css, it doesn't work.
Can anyone figure out why? Thanks.
Add the [L] flag to the first rule. Otherwise it drops through to the second rule and attempts to pass it in r= to index.php. Accessing it via public_html works because of the RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(js|css|img|ico)\/(.+)$ public_html/$1/$2 [L]
# Update: Try using the RewriteCond before this line
# as well as where you have it earlier
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?r=$1 [L,QSA]
I am using CodeIgniter on PHP and it produces the following URLs:
http://my.domain.com/app/index.php?/admin/main/
the /index.php? is redundant so I remove it successfully with the following rewrite rule:
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ ./index.php/$1 [L]
however, I would also like to remove the /app part, but when I do so, I get a 500 error. Is this related to Code Igniter - or how do I rewrite this rule correctly (if possible)? I tried with the following:
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ ./app\/index.php/$1 [L]
Thanks!
Sorry but that is a really old way of writing the htaccess for CI if the htaccess is in the main dir of the project just use the universal htaccess code below (this does not need excluded directories and such like the old one i.e.
RewriteCond $1 !^(index\.php|images|robots\.txt)
so it is also better and like I said universal)
*It will not solve your issue with the directory but this is the best way for CI in handling the index.php removal without having to go back each time you add say a docs folder or something.
Awesome htaccess:
# Customized error messages.
ErrorDocument 404 /index.php
# Set the default handler.
DirectoryIndex index.php
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
Try adding rewritebase RewriteBase /Path/to/directory/DirectoryBelowApps/