I have a rewrite rule:
RewriteRule ^((?!index\.php)[^/]+)/?$ index.php?page=$1 [L]
that rewrites example.com/index.php?page=something to example.com/something.
This rewrite rule doesn't let me into example.com/(some-file-here), for example:
example.com/favicon.ico
example.com/robots.txt
To show up the favicon, I simply put it into /images/favicon.ico and stuff like that, but I prefer a correct configured .htaccess and put favicon.ico and robots.txt into root dir.
What changes do I have to make in this rule so I can access example.com/(some-file-here)?
You need to exclude your real files and folders from the rule, try :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L]
Related
Site Structure
/articles/Employment/Companies.php
/articles/Employment/Companies/.htaccess
/articles/Employment/Companies/index.php
.htaccess file reads
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ index.php [L]
So when you go to
/articles/Employment/Companies/[company type]
It is displaying the index.php page.
The Problem
I'm trying to link to
/articles/Employment/Companies.php
without the .php being displayed, however if I link to
/articles/Employment/Companies
it is going to
/articles/Employment/Companies/
What i'm Ideally Looking For
Understand why I my site is adding the / when linking to folder/hello
to strip out all .php so if you go to /hello it'll display /hello.php apart from in certain directories such as my current .htaccess file is located where /this or /that will display /index.php.
Please try with below, use from rewritecond with your existing rule what I am doing if the request is actually for php file which is not index.php then serve the extension less code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule !index.php$ $1.php [L]
I have some folders in the root dir with an .htaccess file inside to deny for any access(deny from all). And my htaccess in root is:
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
It's working fine except when I get the folders name in url. I want to ignore folders name and just redirect any url to index.php?url.
Could anyone help?
The !-d, !-f, !-l conditions mean only apply the rewrite rule if the access url does not resolve to an existing file, directory or link. So if the directory exists, it won't apply the rule. You need to remove those. You then need to prevent the recursive rewrite of index.php to itself like so :
RewriteRule ^index.php$ - [L]
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
You probably want to also change .+ in the last rule to .* to also match the empty url.
Indeed it seems the inner .htaccess files are parsed first, before the RewriteEngine one. So you will need to remove those .htaccess and rely on the rewrite rule.
I goit this htaccess:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?p=$1&title=$2 [L]
So I can rewrite example.com/123/hey to example.com/?p=123&title=hey
But now I can't call files like example.com/pics/jo.jpg directly any more
Is there any way to call the files directly and keep the rewrite rules by modifying the htaccess or something?
The reason why you can't call your /pics/jo.jpg is because your your pattern ^([^/]*)/([^/]*)$ matches all requests of this form /foo/bar , so when /foo/bar.jpg is requested your rule rewrites it to /?p=foo&title=bar.jpg . To solve this ,you need to exclude your existent files and dirs from the rule using RewriteConds
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ /?p=$1&title=$2 [L]
I've got a site with the following .htaccess rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?id=$1
</IfModule>
It works great but I need to expand it so that IF there is another path taken by the user, I can forward it (but the root path should still work). I tried this, but the site just keeps processing the first RewriteRule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?id=$1
RewriteRule /^(.*)$/^(.*)$ /$1.php?id=$2
</IfModule>
Any ideas?
So the root page could be
domain.com/doug so this is /index.php?id=doug
domain.com/dave so this is /index.php?id=dave
The inner path could be
domain.com/group/object1 so this is /group.php?id=object1
domain.com/group/object2 so this is /group.php?id=object2
domain.com/admin/login so this is /admin.php?id=login
Ok, I think you have to go about it differently.
The easy way would be to just pass everything to index.php, chop up the $_GET['id'], and switch($id[0]) on the root folder ('admin', 'group', etc..) as a parameter in your script.
Perhaps even include("group.php") or admin.php inside the index.
Otherwise you're going to run into the problem of the root url's going to non-intended pages like: doug.php and dave.php
It can be done the current way you're headed, but you'll need to hard code cases for each root folder:
Example:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/admin/(.*)$ /admin.php?id=$1 [L,NC]
RewriteRule ^/group/(.*)$ /group.php?id=$1 [L,NC]
RewriteRule ^(.*)$ /index.php?id=$1
You'll need these above the working RewriteRule line. That line should always be last, since it's the catch-all / nothing-else-matched / default case.
If hard coding the root pages is not an option, (too dam many or always unknown), you'd be better off in the long run to have your index.php just handle everything anyway.
Hope this helps.
Currently this is my .htaccess
RewriteEngine on
#rewrite the url's
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
So, from the index i render a template, and give it a url.
Normally a page would look like this
www.whatever.com/?url=test/page
But with the rewrite it goes
www.whatever.com/test/page
So the question is {
I have an admin section of the site that I want unaffected by this.
So, /admin needs to access the admin folder in the folder tree.
Thanks for the help
-Wes
The best way to do this is to not re-write the URL's of real files and directories on the filesystem. This can be achieved by adding a couple rewrite conditions to your rule:
RewriteCond %{REQUEST_FILENAME} !-s [OR]
RewriteCond %{REQUEST_FILENAME} !-l [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
Now, these mean, respectively, only rewrite urls that are: not a real file (with > 0 size), not a symlink, and not a directory.
Alternatively, you could just make sure your rule does not match your admin directory:
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
The first example is by far the most flexible, however, as it won't interfere with any static files, such as images, etc.