Rewrite rule for subfolder - php

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>

Related

.htaccess in subfolder for removing extension is not working

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>

.htaccess not working - CodeIgniter

I've tried to host a CodeIgniter website in Ubuntu server.
All other websites are working fine without any issues (the sever contains WordPress and Laravel applications). But this particular CodeIngniter website is not taking .htaccess file. I've spend a day to figure out the issue, but no luck.
Here is the details.
Website url structure: http://example.com/website_name
.htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Virtual host entry
<VirtualHost *:80>
ServerName example.com
ServerAlias example.com
DocumentRoot "/var/www/example.com"
<Directory "/var/wwww/example.com">
Options Indexes FollowSymLinks MultiViews
AllowOverride ALL
Order allow,deny
allow from all
</Directory>
</VirtualHost>
CodeIgniter config file
$config['base_url'] = 'http://example.com/website_name/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
And when I'm trying to access the website the output is as follows,
Not Found
The requested URL /website_name/test was not found on this server.
But if I add index.php, the the website is working without any issues. I've tried lot methods and it doesn't worked.
try this .htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteBase /website_name/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php [L]
</IfModule>
Add to your .htaccess
RewriteBase /website_name/
after your RewriteEngine on
I have a feeling MultiViews is probably causing some issues.
Change this in your VHOST
Options Indexes FollowSymLinks MultiViews
to this
Options Indexes FollowSymLinks
And also probably add a rewritebase to .htaccess Rewrite
RewriteBase /website_name/
Restart apache for config changes
Try changing your .htaccess like this.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
Or this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Not the ultimate answer but a good way to test if .htaccess is working at any level.
Create this .htaccess and put in root directory
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^test\.html http://www.google.com/? [R=301,L]
Then direct a browser to
http://example.com/test.html
If you end up on Google then you know .htaccess is working.
A useful way to debug .htaccess is to use its logging function.
Add this to your vhost's configuration:
RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 9
You'll probably need to restart apache to get logging started.
Finally I've figured out the issue by spending an entire day. Here is the solution.
Updated virtual host entry.
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com/
# Additional section added to get the htaccess working in sub folder.
Alias /website_name/ /var/www/example.com/website_name/
<Directory /var/www/example.com/website_name/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
</VirtualHost>

htaccess - displaying 500 Error even enabling the htaccess file in httpd.conf on localhost

.htaccess displaying 500 server error.
Location of file is root folder.
Below is the code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule .signup.php [L]
RewriteRule .* .signup.php [PT,L]
</IfModule>
I've enabled htaccess file in httpd.conf file
that is
<Directory />
Options FollowSymLinks
AllowOverride all
Order deny,allow
Deny from all
Satisfy all
</Directory>
and
LoadModule rewrite_module modules/mod_rewrite.so
Your RewriteBase line is faulty without specifying a directory.
Try either: RewriteBase / -or- removing the line entirely.
If that doesn't work, I think it would be safer to write your .htaccess file like so:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#If we're already on .signup.php, exit so we don't get stuck in a loop
RewriteRule .signup.php - [L]
#If doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#rewrite to .signup.php and read from the top of .htaccess again
RewriteRule .* .signup.php [PT,L]
</IfModule>
That way we avoid redirection loops in case the .signup.php file doesn't exist either..
If that still doesn't work, try testing without the PT flag, it has a tendency of complicating .htaccess if you're not absolutely sure you know what you're doing.

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.

Categories