.htaccess in subfolder for removing extension is not working - php

Below code is for root folder which are specific to few pages to remove extension. And its working.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^About+$ about.php
RewriteRule ^FAQ+$ faq.php
RewriteRule ^Contact+$ contact.php
RewriteRule ^Gallery+$ gallery.php
RewriteRule ^Areas-we-service+$ cities.php
RewriteRule ^(([^/]+/)*[^.]+)$ servicesparse.php?name=$1
And then I've added one php plugin is subfolder called product.
path is root-folder/php-product-catalog-module. Plugin have one another htaccess file in it.
#Fix Rewrite
Options -Multiviews
# Mod Rewrite
Options +FollowSymLinks
RewriteEngine On
RewriteBase /php-product-catalog-module/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# used for viewing single product page
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\/?$ product.php?name=$1&id=$2
# used for php pages such as "yoursite.com/login.php" will become "yoursite.com/login/"
RewriteRule ^([a-z_]+)\/?$ $1.php [NC]
So htaccess for subfolder is not working.
it does not redirect http://cp2.designnrank.com/~wolverin/php-product-catalog-module/login.php to http://cp2.designnrank.com/~wolverin/php-product-catalog-module/login
Rules for other pages of product is not working too.

Are you sure htaccess is enabled? htaccess files are disabled by default in Apache these days, due to performance issues. When .htaccess files are enabled, the web server must check for it on every hit in every subdirectory from where it resides.
Enabling htaccess can be done by replacing "AllowOverride None" with "AllowOverride All" in the <Directory></Directory> directives.
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
A better approach is to put all your rewrite rules into a virtualhost configuration file. Something like:
<VirtualHost *:80>
DocumentRoot /var/www/mywebsite
ErrorLog ${APACHE_LOG_DIR}/mywebsite-error.log
CustomLog ${APACHE_LOG_DIR}/mywebsite-access.log combined
#Fix Rewrite
Options -Multiviews
# Mod Rewrite
Options +FollowSymLinks
RewriteEngine On
RewriteBase /php-product-catalog-module/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# used for viewing single product page
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\/?$ product.php?name=$1&id=$2
# used for php pages such as "yoursite.com/login.php" will become "yoursite.com/login/"
RewriteRule ^([a-z_]+)\/?$ $1.php [NC]
</VirtualHost>

Related

Removing index.php in CodeIgniter using .htaccess file when using your own local domain

I have seen other articles on the same issue. But none of the answers have solved my problem.
I am not using xampp or wamp but have setup php mysql apache seperately. Also I am using my own local domain for instance mytestsite.com.
My .htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /TestCI/
# Canonicalize Codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(HomeController(/index)?|index(\.php|html?)?)/?$ / [R=301,L]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R=301,L]
# Enforce www
# If you have subdomains, you can add them to
# the list using the "|" (OR) regex operator
RewriteCond %{HTTP_HOST} !^(www|TestCI) [NC]
RewriteRule ^(.*)$ http://www.mytestsite.com/$1 [R=301,L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
I have also made following changes in config.php
$config['base_url'] = 'http://mytestsite.com/TestCI/';
$config['index_page'] = '';
$config['uri_protocol'] = 'PATH_INFO'; //AUTO
I have also made following changes in apache2.conf
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
But when i try to access
http://mytestsite.com/TestCI/HomeController/
its showing 404 error but works correctly when using
http://mytestsite.com/TestCI/index.php/HomeController/
I have looked into several similar questions that have been reported solved, but none of the answers have worked for me. Is there anything else that should be done to remove index.php from the url. Kindly respond back. Help highly appreciated.
It may be the htaccess isn't being used.
Put a sytax error in the file. (Append something like hdtbf to the file)
If that doesn't give a 500 error you need to turn allow override all
In your main Apache config
http://httpd.apache.org/docs/2.2/mod/core.html#allowoverride
Replace the following line of code in your .htaccess file
RewriteEngine On
RewriteBase /project/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Just put this code in your .htaccess :
RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Thankyou for helping me... Problem is solved.
I made the following changes in my apache2.conf file
<Directory /Absolute path to .htaccess folder
AllowOverride All
Require all granted
</Directory>
Also I removed the RewriteBase parameter in .htaccess file which is where the actual pbm was.
My current .htaccess file looks like
<IfModule mod_rewrite.c>
RewriteEngine On
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
RewriteCond $1 !^(index.php|resources|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>
I have also made changes in config.php where i changed $config['base_url'] to codeigniter root path

Move all .htacces configurations in cakephp to Apache virtual host

My intention is to speed up the performance of the cake app. I copied all the .htaccess files from my cake app, and despite the configtest saying all is ok, I still get an internal server error when I try and load the page. Is this the correct way to bring in .htaccess files from the app directory?
<Directory /var/www/html/aga-stag>
Options Includes FollowSymLinks
Require all granted
AllowOverride None
Order deny,allow
Allow from all
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^kwiksta\.com [NC]
RewriteRule ^(.*)$ http://www.kwiksta.com/$1 [L,R=301]
RewriteRule ^(red5|oflaDemo) - [L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /app/
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
</Directory>
.htaccess files are valid apache conf syntax, so in principle all you need to do is copy and paste the htaccess file's contents into the right place. However don't blindly do that.
This is more appropriate for handling your kwiksta.com -> www.kwiksta.com redirects:
<VirtualHost *:80>
ServerName kwiksta.com
RewriteEngine On
RewriteRule ^ http://www.kwiksta.com{REQUEST_URI} [R=301,L]
</VirtualHost>
This snippet:
RewriteRule ^(red5|oflaDemo) - [L]
Is better handled by just putting (or symlinking) those folders (assuming that's what they are) into the webroot
And this is all that's required to then handle your CakePHP application:
<VirtualHost *:80>
DocumentRoot /var/www/html/aga-stag/app/webroot
ServerName www.kwiksta.com
AllowOverride None # No need for htaccess files now
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</VirtualHost>
Note that the document root points at the webroot folder as it should for a any and all production installs, which renders 2 of the 3 htaccess files irrelevant.
Be sure that the rewrite rules match the version of CakePHP you are using as they changed over time.

htaccess rewrite rule is not working

All,
I'm trying to redirect the hits via htaccess redirect scripts.
Below is my htaccess scripts.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^post/(.*)/$ post.php?tag=$1 [L]
RewriteRule ^search/(.*)$ search.php?tag=$1 [L]
Issue : Index and search page are getting redirected. Post page is not getting redirected.
Please help on how to fix it
Try this in your root .htaccess:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^post/(.+)$ post.php?tag=$1 [L,NC,QSA]
RewriteRule ^search/(.+)$ search.php?tag=$1 [L,NC,QSA]
Apache out of the box doesn't allow use of htaccess files. If you haven't edited your /etc/apache2/sites-available/default file, you'll need to do so. Look for this chunk of code:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/
</Directory>
And change AllowOverride None to AllowOverride All. After you've changed that, you'll need to restart Apache for the changes to take place (service apache2 restart).
Caveat
Most of what you'd want to do via htaccess files can be done more securely via the Apache configuration files (hence the htaccess files are ignored by default). See this post for details.

htaccess rewriterule not working in codeigniter

Here I am trying to remove index.php from URL in my codeigniter application.
For that, i write .htaccess file like this:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
# RewriteBase /projectname/
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php [PT]
It is working in xx.xx.xx.xxx server and mydomain.com. Here i tried to make a same copy to another server yy.yy.yy.yyy. In this, rewrite URL is not working. If i use 'yy.yy.yy.yyy/index.php/login', its working correctly. But 'yy.yy.yy.yyy/login', it throws me 'The requested URL /projectname/login was not found on this server.' error.
For your main directory .htaccess try this and then remove index.php from config.php
Options +FollowSymLinks
Options -Indexes
<FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
Order deny,allow
Deny from all
</FilesMatch>
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Do not touch the .htaccess files in applications directory's.

Rewrite rule for subfolder

I have the file structure
index.php
.htaccess
news/index.php
news/.htaccess
First .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/news/
RewriteRule . /index.php [L]
Second (news/.htaccess)
RewriteEngine On
RewriteRule . /index.php
Request http://test.t/news/news/61 the handles first index.php but I need to do it the second
I tried a few more options for the first .htaccess, but it did not succeed
Check your Apache config file (httpd.conf) and make sure the directory you are using for your site includes the AllowOverride option.
Example:
<Directory "/Applications/MAMP/htdocs">
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>

Categories