Rewriting urls with Htaccess like filename/param1/param2 etc etc - .htaccess

I'm looking for a solution for the following situation. I'd like to covert the following url
www.example.com/filename.php?p1=1&p2=2 etc
to
www.exmaple.com/filename/1/2 etc
I managed to do this, with index.php, but I'd like to use other file aswell, not only the index. Is it possible? Here's my current code.
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteRule ^(filename)/([^/]+)/([^/]+)$ /$1.php?p1=$2&p2=$3 [L,QSA,NC]
ErrorDocument 404 /404.php
# display no errs to user
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
This works for the php removal. But if I add parameters after it, it gives me error 500

Insert this rule just below RewriteEngine On line:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(filename)/([^/]+)/([^/]+)/?$ /$1.php?p1=$2&p2=$3 [L,QSA,NC]

Related

htaccess rewrite rule, url doesn't work when i add a / at the end and it show error

Options +FollowSymLinks -MultiViews -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{HTTP_HOST} ^themobilesapp.com$ [NC]
RewriteRule (.*) https://www.themobilesapp.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([A-Za-z0-9-]+)$ specification.php?url=$1 [L]
ErrorDocument 404 404error.php
</IfModule>**strong text**
When I open the URL https://www.themobilesapp.com/Motorola-Moto-G4-Play-specifications-7443
it works fine but when i tried to open the URL by adding "/" in the last on the url like https://www.themobilesapp.com/Motorola-Moto-G4-Play-specifications-7443/
I have tried many but it doesn't solve my problem.
I want my URL as https://www.themobilesapp.com/Motorola-Moto-G4-Play-specifications-7443
With your shown samples, please try following Rules in your .htaccess file. Please make sure to clear your browser cache before testing your URLs.
Options +FollowSymLinks -MultiViews -Indexes
DirectorySlash Off
<IfModule mod_rewrite.c>
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{HTTP_HOST} ^themobilesapp\.com$ [NC]
RewriteRule ^(.*)$ https://www.themobilesapp.com/$1 [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/?$ $1.php
RewriteRule ^([\w-]+)$ specification.php?url=$1 [L]
ErrorDocument 404 404error.php
</IfModule>
Also I saw in your previous edit that you need rule like rewriting to given php files with uri, you could cover all those rules with following single rule here:
RewriteRule ^([^/]*)/([\w-]+)/?$ $1.php?url=$2 [L]
This problem has been solved using the below code.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

endless loop in webkits, may be htaccess conflict

Got flamer script (http://provenlogic.xyz/flamer) installed and configured but start to get problem just in Opera and Chrome (in Firefox and IE works fine).
Here is my installation http://chronogym.com/flamer
The same url causes endless loop in Chrome and Opera and works fine in FF.
First question is how it could depend on browser, because I thought that redirections are driven only by server side.
Another question: when adding something like dd('test'); somewhere in controller even in FF script begins to brake connection instead of showing dumping result.
Project is placed in subdir on server: root/flamer. In root I got other project. I suggest that .htacces from that project can cause problems or conflicting with .htaccess from flamer but can't find out the exact cause.
root .htaccess
ModPagespeed off
RewriteEngine On
RewriteBase /
#Options +FollowSymlinks
############################################
## always send 404 on missing files in these folders
RewriteCond %{REQUEST_URI} !^/(_css|_js|memberarea)/
############################################
## never rewrite for existing files, directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
############################################
## rewrite everything else to index.php
RewriteRule .* index.php [L]
ErrorDocument 404 http://www.chronogym.com
RedirectPermanent /coaching.php http://chronogym.com/2-0-coaching_minceur.html
subdir .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.chronogym.com/flamer/$ [NC]
RewriteRule ^(.*)$ http://chronogym.com/flamer$1 [R=301,L]
# Handle Front Controller...
RewriteCond %{REQUEST_URI} !^/admin
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Your middle rule is not correct but that would conflict anyway. You need to use RewriteBase if this .htaccess file is in the flamer folder. Have your rules like this.
RewriteEngine On
RewriteBase /flamer/
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_URI} !^/admin
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

ErrorDocument 404 not working with routing

I tried using this code on .htacces to have my own custom 404 on my page,.
ErrorDocument 404 http://mysite.com/404.shtml
when i visit the page that does not exist on the site. it must show something like this
but this is the one it shows
here is my complete htaccess
<files .htaccess>
order allow,deny
deny from all
</files>
php_value memory_limit 170M
RewriteEngine on
Options +FollowSymlinks
Options -Indexes
RewriteCond %{REQUEST_URI} (.+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
AddDefaultCharset utf-8
RewriteRule (.*) index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.mysite.com.*$ [NC]
RewriteRule ^(.*)$ http://mysite.com%{REQUEST_URI} [L,R=301]
ErrorDocument 404 http://mysite.com/404.shtml
The location of my htaccess is located along with the 404.shtml and the index.php of the site
Removing this code would fix the problem. How can i make it work with routing.
RewriteCond %{REQUEST_URI} (.+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
AddDefaultCharset utf-8
RewriteRule (.*) index.php
Instead of referencing the entire url with ErrorDocument 404 http://mysite.com/404.shtml, name the document relative to your web root. In this case, /404.shtml.
Use these codes in your .htaccess for capture 404 error pages.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.html [L]
</IfModule>
Now your all 404 requests will goto /404.html. You can also use /404.php to code more dynamically. Also make sure that rewrite module is enabled in your apache.

htaccess Rewrite Rule not working without index.php in url

I am using apache2 in ubuntu m/c. I have enabled mod_rewrite and changed Allowoveride All in apache2.conf
Now I am trying to install Bug-Genie application in my server
But following .htaccess rules are not work as excepted.
I can access :
http://localhost/roxsoft/thebuggenie-3.2.6/thebuggenie/**index.php**/wiki
But I can't access : --404 error
http://localhost/roxsoft/thebuggenie-3.2.6/thebuggenie/wiki
Here is my .htaccess file:
# .htaccess file for The Bug Genie
# make sure that magic_quotes and register_globals is always off
<IfModule mod_php5.c>
php_flag magic_quotes_gpc off
php_flag register_globals off
</IfModule>
# rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Example:
# RewriteBase /
# or
# RewriteBase /dev/thebuggenie
# skip all hidden files (starting with a .)
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.(html|wsdl|json|xml)$
RewriteRule .* - [L]
# redirect to front controller
RewriteRule ^(.*)$ index.php?url=$1 [NC,QSA,L]
</IfModule>
# Stop people accessing directories they shouldn't have access to
RedirectMatch 403 ^/\.svn(/|$)
Try this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|assets|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Add the folder which contains the JavaScript and CSS to the rewrite condition
Here is the solution I just managed it with. This may help if anyone had a same problem...
I just removed
and added additional conditions in htaccess
here is it:
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.(html|wsdl|json|xml)$
RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php|gif|ttf|woff)$ ---- ADD FILTER CONDITION.
#RewriteRule .* - [L] ---- COMMENT THIS LINE************
# redirect to front controller
RewriteRule ^(.*)$ index.php?url=$1 [NC,QSA,L]

htaccess rule return funny results in $_GET variable

this is a snippet of my htaccess, at the bottom is the redirect for user accounts.
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ /$1/ [R=301,L]
</IfModule>
Options -MultiViews
Options +FollowSymlinks
Options -Indexes
ErrorDocument 404 /core/error/templates/404/
AddDefaultCharset utf-8
AddCharset utf-8 .html .css .js .xml .json .rss .php
<IfModule mod_rewrite.c>
RewriteRule "(^|/)\." - [F]
</IfModule>
RewriteRule ^favicon.ico favicon.ico [NC,L]
# ----------------------------------------------------------------------
# RewriteRules
# ----------------------------------------------------------------------
# User profile redirect
#RewriteRule ^([^/]+)?$ /user/$1 [NC,R]
RewriteRule ^/([^/]+)/?$ profile.php?user=$1 [NC,L]
if i go to www.foo.bar/samsam it should populate the $_GET['user'] with "samsam" but when i do an echo it actually returns $_GET['user'] = profile.php
any ideas as to why its behaving this way?
You're close, but you need a couple of fixes to that last line. You'll need to remove the leading "/" character in your regexp. You'll also need to filter your last RewriteRule so it only affects non-existent files and directories. So... replace it with the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ profile.php?user=$1 [NC,L]

Resources