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>
Related
i have a problem with the htaccess file from a system which i bought.
The Original File 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>
My modified version looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^city-([^/]+).html searcharea?city=$1&sd=ls [L]
RewriteRule ^city-normal-([^/]+).html searcharea?city=$1&sd=pb [L]
RewriteRule ^city-special-([^/]+).html searcharea?city=$1&sd=oeb [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
The Systems Base Home Link looks like this:
http://www.url.de/store/home
and my .htaccess is completely ignored, i wonder why?
i want to include a seo linking to the system and reffer its url to the function but it doesnt work with this htaccess.
i have this htaccess rules in my other htaccess file from another system and there it works perfectly.
edit:
the rules from original file work, just my rules are ignored. mod_rewrite is enabled
Structure of my dirs:
/
/.htaccess
/index.php -> uses yiiframework to build pages
/core
/core/css
/core/js
This is because your orignal htaccess rules are conflicting with the new rules and they rewrite all non existent requests to index.php. To fix this, you need to reorder your rules .
Try :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^city-([^/]+).html searcharea?city=$1&sd=ls [L]
RewriteRule ^city-normal-([^/]+).html searcharea?city=$1&sd=pb [L]
RewriteRule ^city-special-([^/]+).html searcharea?city=$1&sd=oeb [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Make sure that mod_rewrite is enabled in apache on your server.
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>
I am new to htaccess file and have the following requirement.
I have a dynamic page which handles like this
com/index.php?postname=products
.com/index.php?cat=news&country=india
for these pages I want SEO friendly urls like this
.com/products/ to .com/index.php?postname=products
.com/news/country to .com/index.php?cat=news&country=india
What code do I need to put it in htaccess file?
Do I need to write RewriteCond or RewriteRule or both?
If possible please give me the sample code to do this.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?postname=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?cat=$1&country=$2 [L]
Try this Example for your work.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^postname=(.*)$
RewriteRule ^index\.php$ /postname/%1? [R=301]
RewriteRule ^postname/([^-]+)$ /postname.php?id=$1 [L]
The above code will redirect
index.php?postname=products
to
index/products
Here is the htaccess code that helps you
Save the line .htaccess in your index.php folder or root folder.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([A-Za-z0-9_\-]+)$ index.php?postname=$1 [L]
RewriteRule ^([A-Za-z0-9_\-]+)/([A-Za-z0-9_\-]+)$ index.php?cat=$1&country=$2 [L]
</IfModule>
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
<IfModule mod_gzip.c>
mod_gzip_on No
</IfModule>
It works fine for me.
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.
The main site runs WordPress with .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I'm going to create an CodeIgniter app in subdirectory site.com/online/
If simply add CI .hta file in subfolder it wouldn't work. CI .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|img|styles|js)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Is it possible to combine two .htaccess files or to do something with .htaccess in CI subfolder?
Thank you.
UPD.
Thank you for answers, I've tried all variants, and eventually moved project to subdomain.
First thing, your .htaccess should be like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} !^GET\s/+online/ [NC]
RewriteRule . /index.php [L]
</IfModule>
#Then add this for CI handling in the same .htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !index\.php [NC]
RewriteRule ^online/(.*)$ online/index.php?/$1 [L,NC]
Then you can remove (or rename) .htaccess in the online subfolder.
Try this for the codeigniter .htaccess:
RewriteEngine on
RewriteBase /online
RewriteCond $1 !^(index\.php|robots\.txt|img|styles|js)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Otherwise all requests to that subfolder would be redirected down to wordpress.
Are you setting AllowOverride? According to the docs you need to allow FileInfo overrides for mod_rewrite.