Every single file is not found with .htaccess. Yet, I can normally access any of the files in question. Also .htaccess is in same folder as any of these files.
For example: If I enter https://example.com/post.php it works fine
But if I try with pretty link (leads to same file post.php) for example: https://example.com/vijest/slug/123 server responds with 404, file not found.
Same happens with any other pretty link, files do exit, but when accessed via .htaccess, server responds with 404.
Can you help me please?
RewriteEngine On # Turn on the rewriting engine
<Files ~ "^.*\.([Hh][Tt][Aa])">
Order allow,deny
Deny from all
Satisfy all
</Files>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(css|jpe?g|gif|png|js|ico)$ [NC]
DirectoryIndex index.php
RewriteRule ^vijest/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)?$ post.php?idNum=$2 [NC,L]
UPDATE 1
I copied working example from same server, but other project, and it did not work... But I am 1000000000% certain that these files do exist in specified path.
You need to pass second param to .htaccess like this : post.php?idNum=$1&q=2 first param is uniqe id, second param is url string.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/ index.php [NC,L]
RewriteRule ^vijest/([a-zA-Z0-9_-]+)$ post.php?idNum=$1 [L]
RewriteRule ^vijest/(.*)/([a-zA-Z0-9_-]+)$ post.php?idNum=$1&q=2 [NC,L]
Your url should be saved in db like so : how-to-create-seo-url
and your php link should be like : See More
in post.php page : $_GET['idNum']; AND Then your query echo $_GET['idNum'];
Here is the result : https://ibb.co/YDxQ8Mf and two parameters : https://ibb.co/XSZnSMJ
Make sure if your post.php is in root folder with htaccess.
So in case anyone goes through this thread, my problem was with file name.
Instead of htaccess, it was named htaccesss. If you open shared screenshots you' ll be able to see.
Thank you.
Related
My website has a library / directory path like:
{root}/pages/{files}.php
My index.php path is the following: {root}/index.php
I want, that when people go to (as an example) 'login.php' (which exists in the /pages directory. that the url does not contains the '/pages'.
So:
www.website.com/pages/dashboard.php
Redirects to
wwww.website.com/dashboard.php
And that it is possible that when people access www.website.com/dashboard.php they can access this page.
Sorry for the bad explaining, can't find the good words for it.
Edit : This is my .htacces now
RewriteEngine on
# Rewrite automatic to /index.
RewriteCond %{REQUEST_URI} ^/$
# Second check for if above doesn't do the job [www/https].
RewriteCond %{REQUEST_URI} !^/index [NC]
RewriteRule ^$ /index [R=301,L]
# Delete all the .php and .html extensions from files [url-related].
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Prevent people for looking inside the .htacces file.
<Files .htaccess>
order allow,deny
deny from all
</Files>
And I did try this:
RewriteRule ^/?pages/(.*)$ /$1 [R=301,L]
(Which did redirect me, but still received a 404 error.
I doubt you can do that with .htaccess only. (EDIT: you can, see "EDIT" below)
What you want is to PRETEND that login.php were in the root directory, but actually it is not.
EDIT: I think I mixed it up. You could do it with the .htaccess-file only. But I don't recommend it. You would have to add all redirections per hand (or develop expressions for the different cases).
Instead, I propose to use the following technique. It is basically the start of a self-made Content-Management-System, where the user can create pages on their own and name the URL leading to this page. Also, this all can be extended to an ajax-supporting website to be really up-to-date!
Using .htaccess only
In case you are not interested in the named advantages of the controller-technique, replace the code in your .htaccess-file and your login-page-problem is solved.
RewriteEngine on
#catch www.example.com/login.php and redirect without user's conciousness to www.example.com/pages/login.php
RewriteRule ^(login.php)$ http://www.example.com/pages/login.php [NC,L,QSA]
# Prevent people from looking inside the .htacces file.
<Files .htaccess>
order allow,deny
deny from all
</Files>
In case you want to gain more (and actually helpful) knowledge and want to learn a universal technique of how to start a CMS with the named advantages, continue reading!
Goal of this example
When the user calls the URL "www.example.com/login", the login-page ("login.php") will be loaded.
Setup
You need a .htaccess-file in the root directory.
You need a "controller" in the root directory (I propose index.php).
You need content pages somewhere, in this example a file called "login.php" in root/pages.
Htaccess will redirect all URLs to this controller, also it passes the URL originally called by the user. Based on the passed URL the controller decides which page content to load. In our case it will be login.php.
Controller
path: root/index.php
<?php
function callPage($data) {
if (isset($data['path'])) {
// get the correct file for the given path
switch ($data['path']) {
case "login":
include("/pages/login.php");
break;
default:
// show 404 error if given path is unknown
include("/pages/error.php");
}
}
}
?>
<html>
<head></head>
<body>
<div id="page"><?php callPage($_GET); ?><!-- load called page's content --></div>
</body>
</html>
Example content page
path: root/pages/login.php
<p>I am a happy login page. See, that I don't need any HTML Tags, a body or a head, because I am meant to be used only when mother index.php calls me up and integrates me. Life's so easy!</p>
<p>And do you know what's the best? I can even run PHP inside myself! Awesome, isn't it?</p>
.htaccess-file
path: root/.htaccess
RewriteEngine on
#if called file or directory reallyexists, don't redirect (might lead into a loop)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#redirect every URL (that does not really exist) to index.php
#the URL the user called can be accessed in index.php in $_GET['path']
RewriteRule ^(.*)$ http://www.exampe.com/index.php?path=$1 [NC,L,QSA]
# Prevent people from looking inside the .htacces file.
<Files .htaccess>
order allow,deny
deny from all
</Files>
Impulses
This is of course a very basic example. Currently, there is no advantage in using this technique instead of a simple redirection in the .htaccess-file, rather only disadvantages. But it is now on your own to enhance it. Some possibilities:
index.php: Replace the switch with a database call. It takes the user's url and returns the corresponding "real" URL (the one the user never sees)
Add a title to the page (also saved in database)
Implement an Ajax-support for your website
In the end, this leads to a small system, which can be extended to an own CMS
I have a WordPress installation in my main folder, so my site goes like
www.example.com
Besides that I have a subfolder with a separate static site that you can access with
www.example.com/special
So in the special pages I have a page called
www.example.com/special/my-special-page
It's actually a .php file, but I've removed the extensions in the .htaccess file of that special site with this
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
And this works.
But my client said: I want, when I enter
www.example.com/myawardspage
to go to
www.example.com/special/my-special-page
but to have the above URL (without /special/my-special-page) in it. So in the main .htaccess file (the one that controls the WordPress) I've added
RewriteRule ^myawardspage?$ http://www.example.com/special/my-special-page [NC,L]
So now when you go to
www.example.com/myawardspage
I am redirected to
www.example.com/special/my-special-page
which is great, but I need the URL to look like
www.example.com/myawardspage
So in the .htaccess file of the special page I've added
RewriteRule ^myawardspage/?$ /special/my-special-page [NC]
But the URL remains the same (http://www.example.com/special/my-special-page).
What am I doing wrong here?
Just below RewriteEngine on, place the following lines in your /.htaccess (document root) file:
# Check if the file being requested exists
# in the 'special' directory
RewriteCond %{DOCUMENT_ROOT}/special/$1.php -f
# If so, then rewrite accordingly:
RewriteRule ^([^.]+) special/$1.php [L]
Using this method means that you don't need that rule in the special/.htaccess file - you can safely remove it.
Ok, so you mean when ever you want to hit www.example.com/myawardspage then you want to get data from www.example.com/special/my-special-page while in your address bar your address remains www.example.com/myawardspage To achieve this apply this rule in your WordPress installation's .htaccess file.
RewriteRule ^myawardspage/?$ www.example.com/special/my-special-page [NC,L] # Handle requests for "www.example.com/special/my-special-page"
It wil get you desired data and your problem will be solved. Let me know if that works for you.
Okay...My title is a bit of an exaggeration...
My site is built in PHP, and all the files I'm trying to "require_once" aren't being loaded. The only file I've changed is my .htaccess file. I don't know a thing about .htaccess and what I have is purely from searching the web. What is wrong? Thanks for any help in advance.
RewriteEngine on
<Files 403.shtml>
order allow,deny
allow from all
</Files>
ReWriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
ReWriteRule !index.php index.php [L]
Also, if I comment out the bottom two lines, my site works great.
Well, require_once has nothing to do with .htaccess file: it's a PHP directive, not an Apache one. You have to set correctly the include_path for your files and make sure these directories and files are reachable (i.e., with correct privileges set on them).
If you show the error message you got from failed require, it'd be much more simple to give you a specific advice on how to fix it.
UPDATE If what you need is redirecting all the non-AJAX requests for .php files into index.php, your .htaccess should like this:
RewriteEngine on
RewriteCond %{HTTP:x-requested-with} ^XMLHttpRequest$
RewriteRule . - [L]
RewriteCond %{REQUEST_FILENAME} !-d
ReWriteRule .php$ index.php
This basically means the following: "all AJAX requests - go for what you need, all non-AJAX requests IF you're not going for some directory and are ended with .php - go for index.php instead".
Without checking for .php (or some similar check) you will redirect to index.php all the script loading procedures; and, unless you do it from some external CDN, it's not what would work in your case. )
Try changing the last two lines to this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
If you want your URL's to look something like this (you probably do):
http://yoursite.com/some/path/somewhere
then change the last line to:
RewriteRule ^([^/]*)(.*)$ index.php?first=$1&second=$2
If that's what you want to achieve, ensure that if you're trying to go to:
http://yoursite.com/about
That there isn't actually a folder called about, this line:
RewriteCond %{REQUEST_FILENAME} !-d
Checks to see if a folder with the name "about" exists, if it does, then the page will not redirect, the same goes for files, say you go to:
http://yoursite.com/about.html
If about.html actually exists then the page will not redirect.
Hope that makes sense.
If you need more information, http://jrgns.net/content/redirect_request_to_index seems to be fairly succinct and helpful.
I have a question about using multiple .htaccess files - I couldn't find the answer to this after looking elsewhere on stackoverflow, so I hope you guys can help.
I currently have one .htaccess file in the root of my site, which performs a simple url rewrite:
Options -MultiViews
# CheckSpelling off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [L]
ErrorDocument 404 /index.php
I'm currently working on the second phase of development of this site, and I've made a replica in a subfolder (e.g. www.abcdef.com/new/). The trouble is, at the moment if I click a link on this replica site, it redirects me to the root, original page, whereas I want it to go to the equivalent page in the new/ folder. I've put another .htaccess file in this new/ folder, which however doesn't have any noticeable effect:
Options -MultiViews
# CheckSpelling off
RewriteEngine On
RewriteBase /new/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /new/index.php?url=$1 [L]
ErrorDocument 404 /index.php
So my question is: is it permissible to have another .htaccess file in a subfolder like this? And if so, why aren't the above lines working?
Thanks in advance for any ideas on this!
It's possible to have multiple .htaccess files, and the system is designed to work the way you want it to.
You're setting RewriteBase, which explicitly sets the base URL-path (not filesystem directory path!) for per-directory rewrites.
So it seems like your requests would be rewritten to /new/new/index.php, a path and directory which probably doesn't exist on your filesystem (thus not meeting your RewriteConds) and such is being redirected to your /index.php 404.
As a test, perhaps try changing the ErrorDocument to:
ErrorDocument 404 /new/index.php
If you see rewritten calls go to this then it might indeed be your RewriteBase.
You say
The trouble is, at the moment if I click a link on this replica site,
it redirects me to the root, original page, whereas I want it to go to
the equivalent page in the new/ folder.
Could it be that you are using absolute links in your pages and not relative ones? For instance if a link looks like "/sample", when in your main site it will link to http://.../sample and the same is true if the link is inside a page under "/new/". If you'd use just "sample" then that would resolve as http://..../sample or http://...../new/sample, depending on the URL of the page.
Having a second htaccess file in a subdirectory shouldn't be an issue, and as far as I can tell, your two look okay.
Are you sure the links in the site are correct? (ex, they are /new/foo, not just /foo)?
To start, I am a beginner with PHP and .htaccess. Here is my dilemma...
I have built dynamic pages and used htaccess to rewrite the urls. There are 3 types of pages... Examples:
State: example.com/massachusetts-colleges.html
City: example.com/massachusetts-colleges/boston-ma-colleges.html
College: example.com/massachusetts-colleges/boston-ma-colleges/harvard.html
The problem is that pages are being requested (from old linking structure probably) that shouldn't exist such as:
example.com/boston-ma-colleges.html
The state urls are stored in a locations table in the database (stateSlug = massachusetts-colleges). The city urls are also stored in the locations table in the database and the corresponding state slug is also stored with that city (citySlug = boston-ma-colleges and stateSlug = massachusetts-colleges). The Colleges are stored in a different table and use ID's to correspond with the cities.
How can I use .htaccess to prevent any "OTHER" urls from being accessible (page displays template and no data), and show a 404 page (or redirect to home page)?
This is what my .htaccess file looks like now:
RewriteEngine on
RewriteRule ^([^/\.]+)\.html?$ php/statePage.php?stateSlug=$1 [L]
RewriteRule ^([^/\.]+)-colleges/([^/\.]+)\.html?$ php/cityPage.php?citySlug=$2&stateSlug=$1 [L]
RewriteRule ^([^/\.]+)-colleges/([^/\.]+)/([^/\.]+)\.html?$ php/collegePage.php?collegeSlug=$3&citySlug=$2&stateSlug=$1 [L]
Again, I am somewhat new to the htaccess and php languages. I would appreciate any help in this matter.
Thank you!
Assuming all of your content is matched by one of the above URLs, you can simply forbid access to everything else by adding another rule to the end:
RewriteRule ^(.*)$ - [F]
To avoid messing with requests your images and CSS, you should add:
RewriteCond %{REQUEST_FILENAME} !\.(jpg|png|gif|js|css)$ [NC]
RewriteRule ^(.*)$ - [F]
If you have directories which should be made inaccessible, place a .htaccess in them with only the following:
Order deny,allow
deny from all
Try the following, but replace index.php with your 404 page:
Order allow,deny
Allow from all
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$l [L]
This should redirect all file and directory requests that were not found to index.php or your choice of script file, a 404 page for example. Just remember to set the headers to a 404 response code.
I got this from the CodeIgniter wiki page at http://codeigniter.com/wiki/mod_rewrite/
Their wiki page was very helpful when I tried to solve a similar problem.
rewrite all your public files to fake directory /allow/.., display 404 for any request which is not in /allow/.. directory. Finally rewrite files from /allow/.. to real directory.
It's great workaround IMO :) Better than using deny all in every dir...