Error Re Write Rule - regex

I am using a rewrite rule that got off a learning video
but it is not working properly
Options +FollowSymLinks
Options -Indexes
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]
I would like it to pick up if just type in
http://www.example.com/hmvctest/helloworld
but it only works if i type in
http://www.example.com/hmvctest/index.php/helloworld
currently only shows "No input file specified" I use codeigniter and cpanel I have tried the two answers below and still no luck my site uses https

Try replacing your rules with this:
Options +FollowSymLinks -Indexes -MultiViews
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(index\.php|images/|robots\.txt) [NC]
RewriteRule ^ index.php%{REQUEST_URI} [L]

Use this code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /www.example.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>

Related

Apache .htaccess file not rewriting properly

I have an .htaccess file. It needs to rewrite URLs in two cases: for the MVC framework and in a more special case. I want to rewrite any requests to "/resources/newspaper" if the user doesn't have a cookie named "cfnw-hash". I've tried putting the code before the MVC code and after it. Doesn't work. It worked before switching over to the MVC framework, though I really don't have enough .htaccess knowledge to see if that's causing the issue. Here's code:
Options -Indexes
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteBase /
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_COOKIE} !cfnw-hash [NC]
RewriteRule \/resources\/newspaper.* http://www.example.com/error/401 [R=401,NC,L]
Combine your rules and I think you RewriteRule might not be matching because of the prepending / and also the cookie matching. Try this. Notice the cookie part that you update with your cookie.
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !cookie_name=specific_value; [NC]
RewriteRule ^resources/newspaper/?(.*) http://www.example.com/error/401 [R=401,NC,L]
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
Have it this way:
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !cfnw-hash [NC]
RewriteRule ^resources/newspaper /error/401 [R=401,NC,L]
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
Test this after clearing your browser cache.

htaccess redirect not working. gives 404

I'm using slim framework and it has following .htaccess
Options -Multiviews
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)?$ index.php?url=$1 [NC,L,B,QSA,NE]
</IfModule>
I'm migrating an old site which has urls like index.php?q=about.
Now I want both index.php?q=about and /about/ to go to same page. So I tried this
Options -Multiviews
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^q=
Rewriterule ^index.php?q=(.*)$ index.php?url=$1 [R=301,NC,B,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)?$ index.php?url=$1 [NC,L,B,QSA,NE]
</IfModule>
But I'm getting 404 error. Can anybody help me out?
Update:
mod_rewrite is enabled and /about/ is working fine. I need index.php?q=about to show the same page.
You can use:
Options +FollowSymLinks -Multiviews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# redirect /index.php?q=about to /about
RewriteCond %{THE_REQUEST} \s/+index\.php\?q=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
</IfModule>

htaccess URL rewrite - requested URL not found on server

Need to hide one folder while loading like Uniimart1609/S/page.php to Uniimart1609/page.php
in linux
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule Home_Page.php$ S/Home_Page.php
</IfModule>
is redirected http://Uniimart1609/Home_Page.php this is working fine, but when i rewrite the htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /S/$1.php [L,NC,QSA]
</IfModule>
getting The requested URL /S/Home_Page.php was not found on this server. Pleas help
Have your rule like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /Uniimart1609/
RewriteCond %{THE_REQUEST} /S/(\S*) [NC]
RewriteRule ^ %1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/S/$1\.php -f [NC]
RewriteRule ^([^./]+)/?$ S/$1.php [L]
</IfModule>

Htaccess redirect Laravel 4

I have a Laravel 4 installation, and I used the boilerplate's htaccess to redirect my site from:
site.com.br to www.site.com.br (notice the www).
But when I use a route such as site.com.br/TITLE-OF-MY-POST I am redirected to:
www.site.com.br/index.php instead of www.site.com.br/TITLE-OF-MY-POST"
Does anyone know why? Here my htaccess rules:
Laravel's rule
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Boilerplate's rule
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
Change order of your rules:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
In general keep your 301 rules before your internal rewrite rules.

htaccess human readable

I have a site which already defined htaccess for making url nice
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
Options -indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php
Everything works fine (there is cms in the root folder).
Now I want to create a folder inside root and make another rules for it:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
Options -indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !tip [NC]
RewriteRule (.*) index.php [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^tip/([^/]+)/? /tip/?id=$1
So I want to redirect everything from url: .../tip/?id=N -> ../tip/N
It seems to work fine, id is passed, data is loaded BUT. All the urls are wrong inside site (javascript, css). They aren't loaded.
Look at: http://wincode.org/tip/3
For example, code: <script defer src="js/filtrify.js"></script> produces: http://wincode.org/tip/js/filtrify.js but if you will try to load it in another tab, it will pass js/filtrify.js as id argument, I think. How to fix this?
A. Change your .htaccess code to this in $DOCUMENT_ROOT/.htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
Options -indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^tip(/.*|)$)^.*$ /index.php [L,NC]
B. Change your .htaccess code to this in $DOCUMENT_ROOT/tip/.htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /tip
Options -indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/?$ /tip/?id=$1 [L,QSA]
I just improved little bit anubhava reply, this one is based on root folder example, but you can use same approach for other dir that you need.
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Options -indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^tip(/.*|)$)^.*$ /index.php [L,NC]
</IfModule>
<IfModule !mod_rewrite.c>
# Mod_rewrite not installed, redirect all 404 errors to index.php.
ErrorDocument 404 index.php
</IfModule>