.htaccess rules in a sub directory - php

I've a folder named test, I can access it via http://domain.com/test. I'm trying to send every requests of http://domain.com/test/XXX (XXX = every thing) to a php file.
I used this in test/.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test
RewriteRule ^(.*)$ /control.php?path=$1 [L]
</IfModule>
control.php is in test folder too. But it doesn't work, i don't know in which rule i did a mistake! now every http://domain.com/test/XXX requests shows a 404 not found page!

Try this code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ control.php?path=$1 [L]
</IfModule>

Related

How do i shorten my url like facebook do to users profile page

I am trying to shorten my website url of a newly added page event like
mysite.com/asoebi/myevent instead of mysite.com/asoebi/index.php?event=myevent
but every time i try to change my .htaccess file it give an error
here is .htaccess code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^index/(\w+)$ ./index.php?event=$1
</IfModule>
You have to enable mod_rewrite in apache for this to work.
Put this one in the .htaccess file in the /public_html/asoebi/ folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /asoebi/
RewriteRule ^(.*)$ index.php?event=$1 [L]
</IfModule>
Also remove the last rewriterule from the main htaccess file you posted above

htaccess forward all users to a single file

Good day,
I am using a framework that uses a .htaccess file to forward all requests to a sub file core/index.php
I used the following .htaccess file code :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ core/ [L]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) core/$1 [L]
</IfModule>
This worked great I thought until I noticed that the pages got loaded multiple times.
And this is what I want to prevent.
So I had a little look at this forum.
And I found a solution that the space between (.*) and core could be the reason.
Hence I removed it.
And to my delight the multiple db inserts stopped.
However now I have found that my requests do not get forwarded correctly.
Only the main request (www.myapplication.com/) gets forwarded correctly (index.php)
Whenever I am adding something like : (www.myapplication.com/admin) he bugs out and cannot find the page.
I hope anyone could tell me what to modify in this code :
RewriteEngine on
RewriteRule ^$ core/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) core/$1 [L]
Thanks in advance!!
I also note that whenever I change the (.) core/$1 [L] line to (.)core/$1 [L] that not only my (sub)pages stopped working but for the index.php the load times of my index.php file where split in half.
Hence my page truly gets reloaded multiple times.
Edit
As pointed out by a question below the system also uses a .htaccess file inside the /core directory.
This file has the following contents
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
Edit
I think it would come handy to make a small summary. If requested for a file or path I want the server to check if it is
available on the server.
www.myapplication.com/flower.jpeg and the server can find it it should display the flower.jpeg file.
If the server CAN NOT find the flower.jpeg file I want the server to forward the request to /core/index.php?url=flower.jpeg
I am using two .htaccess files
in the **public directory** (main directory)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ core/ [L]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) core/$1 [L]
RewriteCond %{REQUEST_URI} !core/
</IfModule>
Second file in the core directory
core/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
The issue: my pages are in a loop hence the page index.php get's executed with every server request. E.G. 10 images on a page means 10
requests extra. Same for js and CSS files.
edit:
Solved!!!
This is the solution!!
DirectoryIndex core/index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ core/index.php?url=$1 [QSA,L]
</IfModule>
You have to add
RewriteCond %{REQUEST_URI} !core/
to the RewriteCond block to prevent redirection from the target to the target.
It would be better if your target would be more specific, for example if you could specify /core/ instead of core/ (do you have a core subfolder in every folder?). Also, you will have to exclude image folders.
Try this in /core/.htaccess:
ErrorDocument 404 default
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)/?$ index.php?url=$1 [QSA,L]
I found a solution. The working code is :
DirectoryIndex core/index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ core/index.php?url=$1 [QSA,L]
</IfModule>

Apache .htaccess rewrite for mutiple pages

I am working on a new script, I am using mod_rewrite to create permanent links. But I also want to have a login page that goes to a different main page.
ie.
http://www.example.com/dashboard
will load index.php
but
http://www.example.com/login will load login.php
currently my .htaccess looks like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Not sure the best way to do what I would like. Tried to look around and couldn't find this example.
I think that all You need is just .php rewrite, so that You can use php pages with or without .php extension. The rest (redirecting) You should do from php scripts.
Here is how .htaccess should look like in this case.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [QSA,L]
</IfModule>

htaccess prevent direct page load

I am trying to prevent direct access to all php files on my site except index.php using htaccess. I've got following code so far, however it still does not prevent form accessing php files directly.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php?page=$1 [L]
</IfModule>
My Solution:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(index\.php|.*\.css|.*\.js|.*\.png|.*\.gif|.*\.jpg)
RewriteRule ^(.*)$ index.php?page=$1 [L]
</IfModule>
That's because of these lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Where you are explicitly telling the rewrite module to NOT run the following rules if the requested resource is a file or directory
However, I agree it's best to move them out of the web root entirely.

Remove index.php from Codeigniter URL in subfolder with Wordpress in root

I'm having some issues with a Codeigniter app in a subfolder on my server along with a Wordpress install in the document root. If I hide the index.php of the Codeigniter URL with .htaccess
/codeigniter/.htaccess
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|user_guide|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
And get Wordpress to ignore /codeigniter with RewriteCond %{REQUEST_URI} !^/(codeigniter).*$
/.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/(codeigniter).*$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Then navigate to www.mysite.com/codeigniter/my_controller/my_function, I get a 404 error No input file specified. I found here, that adding a ? after RewriteRule ^(.*)$ index.php solves the Codeiginiter 404 error.
/codeigniter/.htaccess
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
However I'm now getting a Wordpress 404 error, so it seems if I replace index.php with index.php? in /codeigniter/.htaccess, the Wordpress rewrite rule in /.htaccess, RewriteCond %{REQUEST_URI} !^/(codeigniter).*$ gets ignored. Is there a better way to handle this?
Thanks in advance!
The RewriteBase in the codeigniter directory should say /codeigniter/
RewriteBase /codeigniter
Otherwise the rewrite to index.php ends up going to wordpress' router.

Categories