htaccess rewrite rule for url with querystring - .htaccess

I just did a huge makeover to a site and changed the framework. Because of that, the structure of urls changed. This is not a huge problem except for one url which is used a lot. I need to redirect user from the old address to the new one, and I just cannot figure out how to do that.
Old url: domain/?page/subpage/subsubpage.html?param=[parameter]/
New url: domain/controller/function/[parameter]/
My rewrite rules currently are this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [L]
RewriteRule ^/\?page/subpage/subsubpage.html\?param=(.*)$ /controller/function/$1 [L]
but it does not work, no redirect is happening.
Edit: Added the whole htaccess file! This currently gives me error 500 ( "Request exceeded the limit of 10 internal redirects due to probable configuration error.")
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Allow asset folders through
RewriteRule ^(fuel/modules/(.+)?/assets/(.+)) - [L]
RewriteRule ^(fuel/modules/(.+)?/tuki/(.+)) - [L]
RewriteRule ^(fuel/modules/(.+)?/wiki/(.+)) - [L]
# Protect application and system files from being viewed
RewriteRule ^(fuel/install/.+|fuel/crons/.+|fuel/data_backup/.+|fuel/codeigniter/.+|fuel/modules/.+|fuel/application/.+) - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/\?page/subpage/subsubpage.html\?param=(.*)$ /controller/function/$1 [L]
RewriteRule .* index.php/$0 [L]
# Prevents access to dot files (.git, .htaccess) - security.
RewriteCond %{SCRIPT_FILENAME} -d
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\." - [F]
</IfModule>
Options -Indexes

With your shown samples/attempts could you please try following.
Please make sure you clear your browser cache before testing your URLs.
Options +FollowSymLinks
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine ON
RewriteBase /
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Prevents access to dot files (.git, .htaccess) - security.
RewriteRule \.(git|htaccess) - [F]
# Allow asset folders through
RewriteRule ^fuel/modules/(assets|tuki|wiki) - [L]
# Protect application and system files from being viewed
RewriteRule ^fuel/(install|crons|data_backup|codeigniter|modules|application)/.+ - [F,L]
##Any non existing directory OR files whose URI srarts from page rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/\?page/subpage/subsubpage.html\?param=(.*)$ /controller/function/$1 [L]
####If any non existing directory OR file is requested then come here:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php/$0 [L]
###New rule added by me here.....
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
ReWriteCond %{QUERY_STRING} .*=([^/[]*)/?$
RewriteRule ^ controller/function/%1 [L]
</IfModule>

Related

htaccess delete and ban all extensions and add / to the end of the URL

I am making a small cms in php. Through htaccess I would like to delete and block all extensions (.php .html. Htm etc.) and add a / at the end of the url.
ex. www.miosito/ article-name/
Options -Indexes
DirectoryIndex home.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ core.php
this is my current htaccess.
Sorry for my bad English and thank you very much in advance
With your shown samples/attempts, could you please try following htaccess Rules file. Please make sure to clear browser cache before testing your URLs.
RewriteEngine ON
##Excluding Indexes and MultiViews options here.
Options -Indexes -MultiViews
##Rule for url http://localhost:80/test here.
RewriteRule ^(test)/?$ $1/category [R=301,L]
RewriteRule ^(test/category)/?$ $1.php [NC,L]
##Rule for forbidding html OR htm OR php extension files.
RewriteRule \.(html|php|Htm)/?$ - [F,NC,L]
##Rule for adding / at end of urls.
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
##Rules for non-existing pages to be served here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ core.php [QSA,L]
Try out this code in site root .htaccess after completely clearing your browser cache:
Options -Indexes
DirectoryIndex home.php
RewriteEngine On
# block direct access to .php, .html, .html extensions files
RewriteCond %{THE_REQUEST} \.(html?|php)[?\s/] [NC]
RewriteRule . - [F]
## Adding a trailing slash
RewriteCond %{ENV:REDIRECT_STATUS ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,NE,R=301]
# core handler
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . core.php [L]

htaccesc rewrite rule does not work

<Files ~ (\.php)>
Options +SymLinksIfOwnerMatch
</Files>
AddDefaultCharset utf-8
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/([0-9]+)$ index.php?lang=$1&menu=$2&submenu=$3 [L]
origin url: example.com/index.php?lang=de&menu=1&submenu=0
new url: example.com/de/1/0
new url load main page, but need another page
<Files ~ (\.php)>
Options +SymLinksIfOwnerMatch
</Files>
AddDefaultCharset utf-8
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/([0-9]+)$ index.php?lang=$1&menu=$2&submenu=$3 [L]
If you follow this, you told Apache to take everything that is not a file or a directory in the file system and rewrite it to index.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
So that's what it is doing.
To make this do what you want, you have to make it like this:
<Files ~ (\.php)>
Options +SymLinksIfOwnerMatch
</Files>
AddDefaultCharset utf-8
RewriteEngine on
RewriteBase /
# if file/director does not exist, and if it matches the pattern
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/([0-9]+)$ index.php?lang=$1&menu=$2&submenu=$3 [QSA,L]
# then if it did not match the pattern, send it to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
Note that you did not provide enough information to understand what you are actually trying to achieve. But my guess is it's something like this. I'm also assuming that you want to append the values to the query string of index.php (QSA), which is usually what you want.
But it's not really possible to guess further until you specify what all the urls are supposed to do.
Note that by matching anything not file/directory, and rewriting it without further argument as the last command to index.php (L), that ends the request processing, it will never hit the second case.

multiple parameters friendly URL rewriteRule

I need help on rewriteRule for multiple parameters as follow:
sitename.com/project.php?t=value1&a=value2
to become
sitename.com/project/value2/value1
but somehow I was unable to resolve the problem and the page shown 500 Internal Server Error
my htaccess file:
DirectoryIndex index.php
Options -Indexes
<Files *htaccess>
Deny from all
</Files>
<files page>
ForceType application/x-httpd-php
</files>
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^cp/edit-agent/([^/\.]+)/?$ cp/edit-agent.php?name=$1 [L]
RewriteRule ^([^/\.]+)/?$ agent.php?name=$1 [L]
#rule to handle example.com/123/sys
RewriteRule ^project/([0-9]+)/([^/\.]+)/?$ project.php?a=$1&t=$2 [L,QSA]
</IfModule>
Please help.
You're rules look ok for the most part, but you have 2 problems. The first and most obvious problem is that you have 2 conditions that are only applied to the first rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
is only applied to this rule:
#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
When it also needs to be applied to this rule:
RewriteRule ^([^/\.]+)/?$ agent.php?name=$1 [L]
Conditions only get applied to the immediately following rule, so you need to duplicate them.
The other problem isn't so obvious, but this is probably what's causing the 500 error, is this condition:
RewriteCond %{REQUEST_FILENAME}\.php -f
THe problem is that you have requests like: /project/123/abcd and you have the file /project.php. The %{REQUEST_FILENAME} variable also takes into account PATH INFO, so if you just stick .php to the end, it will actually check: /project.php/123/abcd and PASS the -f check. BUt in the rule itself, you're appending it to the end, thus: project/123/abcd.php. Then the next time around, the same condition passes again, then .php gets appended to the end again: project/123/abcd.php.php, thus infinite loop.
So you need to change your rules to look like:
DirectoryIndex index.php
Options -Indexes -Mutiviews
<Files *htaccess>
Deny from all
</Files>
<files page>
ForceType application/x-httpd-php
</files>
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#resolve .php file for extensionless php urls
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^cp/edit-agent/([^/\.]+)/?$ cp/edit-agent.php?name=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/\.]+)/?$ agent.php?name=$1 [L]
#rule to handle example.com/123/sys
RewriteRule ^project/([0-9]+)/([^/\.]+)/?$ project.php?a=$1&t=$2 [L,QSA]
</IfModule>

Laravel htaccess

I've setup a new install of Laravel on my local. It appears there are issues with htaccess or Apache settings. I've researched for a number of hours and tried everything I read.
OSX Lion 10.7.5
MAMP 3.0.5
PHP 5.5.10
mod_rewrite is being loaded.
My development server works with other sites. This is the first time I am trying Laravel 4.
I get a 403 Forbidden on the welcome page which is located at website.dev:8888/
Apache gives me this error: Directory index forbidden by Options directive
Here is my .htaccess file content:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Here are a few additional actions I've taken:
AllowOverride is set to All in httpd.conf
Added virtual host code section to httpd-vhosts.conf
verified that the hosts file contains a line for the site 127.0.0.1 website.dev
I've also tried various lines in the htaccess which I found in articles and I also restarted apache each time I made changes to the conf files. No routes work. When I go to website.dev:8888/public I get a blank page, no error. If I go to a route I created such as website.dev:8888/users I get a 404 not found error.
Thank you for your help!
This solution worked fine, best solution ever for me. Paste this code into root htaccess. That's all. Leave all other files as they are
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
</IfModule>
In the root path create a .htaccess file with
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/$1
#RewriteRule ^ index.php [L]
RewriteRule ^(/)?$ public/index.php [L]
</IfModule>
In public directory create a .htaccess file
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
if you have done any changes in public/index.php file correct them with the right path then your site will be live.
what will solve with this?
Hosting issue of laravel project.
Css not work on laravel.
Laravel site not work.
laravel site load with domain/public/index.php
laravel project does not redirect correctly
Answer
Heres what I found that worked. Delete the .htaccess file inside the /public directory and then put the following into the laravel installation's document root.
Clarification
So for clarification if your laravel application is installed at /public_html/laravel you will look inside /public_html/laravel/public and delete the .htaccess file there. Then inside the /public_html/laravel directory make a new file called .htaccess
Copy the code below to a new .htaccess file in the doc root
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /public/$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ public/index.php [L]
</IfModule>
Please note
As Concordus Applications noted in their answer here make sure that the Loadmodule mod_rewrite is uncommented (make sure there is not a # symbol behind it in your apache config file)
Concordus Applications also noted that you should have AllowOverride set to the appropriate setting. The example they gave was the following.
<Directory "/some/absolute/path/htdocs">
Options Indexes Includes FollowSymLinks MultiViews
AllowOverride AuthConfig FileInfo
Order allow,deny
Allow from all
</Directory>
Please make sure to restart your apache server if you made any changes to your apache config file.
The accepted answer worked but not with laravel passport authentication.
There is a need to add some header tweaking:
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
The framework ships with a public/.htaccess file that is used to allow URLs without index.php. If you use Apache to serve your Laravel application, be sure to enable the mod_rewrite module.
If the .htaccess file that ships with Laravel does not work with your Apache installation, try this one:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Make sure you have .htaccess files in both /wwwroot and /public. The content is different though:
wwwroot/.htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/$1
#RewriteRule ^ index.php [L]
RewriteRule ^(/)?$ public/index.php [L]
</IfModule>
wwwroot/public/.htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
This .htaccess will remove /public/ from your URL and force https://
Put this .htaccess in your root folder without renaming server.php file to index.php
this .htaccess will do everything
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^(.*)$ public/$1 [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Simple Solution Copy and Paste The Code Below Into Your .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase/
RewriteRule ^-$public/index.php[L]
RewriteRule ^((?!public/).*)$ public/$1 [L]
</IfModule>
Create new .htaccess file in root folder.
and put this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
</IfModule>
and add this code in .htaccess at end in public/.htaccess file
RewriteCond %{THE_REQUEST} \s/+(.+/)?public/(\S*) [NC]
RewriteRule ^ /%1%2? [R=301,L,NE]

.htaccess conflict

This is my first question.
I have an wp-site on the root directory and it's own .htaccess file.
RewriteEngine off
<IfModule mod_suphp.c>
suPHP_ConfigPath /home/katambi/public_html/php.ini
<Files php.ini>
order allow,deny
deny from all
</Files>
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
On another directory I've SocialEngine network site. It also has .htaccess file.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
# Get rid of index.php
RewriteCond %{REQUEST_URI} /index\.php
RewriteRule (.*) index.php?rewrite=2 [L,QSA]
# Rewrite all directory-looking urls
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*) index.php?rewrite=1 [L,QSA]
# Try to route missing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} public\/ [OR]
RewriteCond %{REQUEST_FILENAME} \.(jpg|gif|png|ico|flv|htm|html|php|css|js)$
RewriteRule . - [L]
# If the file doesn't exist, rewrite to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rewrite=1 [L,QSA]
</IfModule>
Problem: when i enter to package and plugins page it display 404 error. =(
When i delete code between
# BEGIN WordPress
and
# END WordPress
wp-site down but social network runs correct.
Thanks for your answers.
The code between # BEGIN WordPress and # END WordPress is redirecting all request to root directory's index.php. You need to make sure that requests to SocialEngine's directory is not being redirected. So you can try this solution- .htaccess & Wordpress: Exclude folder from RewriteRule

Resources