custom url in php using htaccess (url rewriting) - .htaccess

i am new in url rewriting in php htaccess. currently my url as like http://localhost/htaccess/Movie/my-parameter-value
but i want to http://localhost/htaccess/my-parameter-value i want remove Movie (movies is from action source name ex "movie.php");
Thanks In Advance
here is my code :
#Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R=302]
# To externally redirect /dir/foo.php?name=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?name=([^&\s]+) [NC]
RewriteRule ^ %1/%2? [R,L]
# To internally forward /dir/foo/12 to /dir/foo.php?name=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)?$ $1.php?name=$2 [L,QSA]

Have it like this:
#Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R=302]
# To externally redirect /dir/foo.php?name=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s(.*)/Movie\.php\?name=([^&\s]+) [NC]
RewriteRule ^ %1/%2? [R,L]
# /htaccess/Movie.php?name=movie-name => /htaccess/movie-name
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?name=([^&\s]+) [NC]
RewriteRule ^ %1/%2? [R,L]
# To internally forward /dir/foo/12 to /dir/foo.php?name=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?name=$2 [L,QSA]
# handle /htaccess/movie-name internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ Movie.php?name=$1 [L,QSA]

Related

URL rewriting for two parameters using .htaccess page not found 404

I am trying to write custom URL for 2 parameters
http://localhost/htaccess/site-list-pune-pune/xcxc
htaccess is my application fordername
but i get this error "Object not found!"
may be i missing in last rule please help me
here is my code :
#Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R=302]
# To externally redirect /dir/foo.php?name=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s(.*)/single-page\.php\?name=([^&\s]+)&dd=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3? [R,L]
# /htaccess/Movie.php?name=movie-name => /htaccess/movie-name
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?name=([^&\s]+)&dd=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%2? [R,L]
# To internally forward /dir/foo/12 to /dir/foo.php?name=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?name=$2&dd=([^&\s]+) [L,QSA]
# handle /htaccess/movie-name internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ single-page.php?name=$1&dd=([^&\s]+) [L,QSA]
internal url : http://localhost/htaccess/single-page.php?name=pune-site-list-pune&dd=xcxc
Replace your last rule by this rule:
# handle /htaccess/movie-name internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ single-page.php?name=$1&dd=$2 [L,QSA]
([^&\s]+) cannot be used in replacement as this this regex to capture values in RewriteCond.

Htaccess issue - url with multiple parameters

I use this htaccess for multiple parameters like bank, state,district & branch.
But it only format bank not others...???
I need it for:
www.domain.com/bank/ .....(It works)
www.domain.com/bank/state/
www.domain.com/bank/state/district/
www.domain.com/bank/state/district/branch/
My htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?bank=([^&\s]+)
RewriteRule ^ /%2/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?bank= ([^&]+)&state=([^&]+)
RewriteRule ^ /%2/%3/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?bank=([^&]+)&state=([^&]+)&district=([^&]+)
RewriteRule ^ /%2/%3/%4/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?bank=([^&]+)&state=([^&]+)&district=([^&]+)&branch=([^&]+)
RewriteRule ^ /%2/%3/%4/%5/? [L,R=301]
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.*)
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
Kindly provide solution for this...Thanks & Regards.
You need to tweak your regex and reverse order of redirect rules:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index\.php\?bank=([^&\s]+)&state=([^&\s]+)&district=([^&\s]+)&branch=([^&\s]+)\s
RewriteRule ^ /%2/%3/%4/%5/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index\.php\?bank=([^&\s]+)&state=([^&\s]+)&district=([^&\s]+)\s
RewriteRule ^ /%2/%3/%4/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index\.php\?bank= ([^&\s]+)&state=([^&\s]+)\s
RewriteRule ^ /%2/%3/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index\.php\?bank=([^&\s]+)\s
RewriteRule ^ /%2/? [L,R=301]
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.+)$
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
Make sure to clear your browser cache before testing this change.

again a new .htaccess issue

One of my URL has this /index.php?page=cfeedback&status_id=2&cf_store=8 ... After applying this htaccess it shows only /index.php?page=cfeedback in the URL.
Anything can be done for this?
Some pages also are just /index.php?page=dashboard.
My .htaccess should support both in this fashion.
/dashboard for URLs /index.php?page=dashboard
/cfeedback/2/8 for URLs /index.php?page=cfeedback&status_id=2&cf_store=8
This is a continuation of this issue: .htaccess file is not working on server
My .htaccess code is
Options +FollowSymLinks -MultiViews
RewriteEngine on
# use appropriate rewrite base
RewriteBase /mydir/admin/
RewriteCond %{REQUEST_METHOD} !^(TRACE|TRACK|GET|POST|HEAD)$
RewriteRule ^ - [F]
RewriteCond %{THE_REQUEST} /index\.php\?page=([\w-]+) [NC]
RewriteRule ^ %1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?page=$1 [L,QSA]
I tried adding the following code for urls having index.php?page=$1&status_id=$2 . Please let me know what I am doing wrong.
RewriteCond %{THE_REQUEST} /index\.php\?page=([\w-]+)&status_id=([\w-]+) [NC]
RewriteRule ^ %1/%2? [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?page=$1&status_id=$2 [L,QSA]
What am I doing wrong here?
Options +FollowSymLinks -MultiViews
RewriteEngine on
# use appropriate rewrite base
RewriteBase /mydir/admin/
RewriteCond %{REQUEST_METHOD} !^(TRACE|TRACK|GET|POST|HEAD)$
RewriteRule ^ - [F]
RewriteCond %{THE_REQUEST} /index\.php\?page=([\w-]+)&status_id=([\w-]+)&cf_store=([\w-]+) [NC]
RewriteRule ^ %1/%2/%3? [L,R]
RewriteCond %{THE_REQUEST} /index\.php\?page=([\w-]+)&status_id=([\w-]+) [NC]
RewriteRule ^ %1/%2? [L,R]
RewriteCond %{THE_REQUEST} /index\.php\?page=([\w-]+)\s [NC]
RewriteRule ^ %1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?page=$1&status_id=$2&cf_store=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?page=$1&status_id=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?page=$1 [L,QSA]
Insert new set of rules before earlier rules:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# use appropriate rewrite base
RewriteBase /mydir/admin/
RewriteCond %{REQUEST_METHOD} (TRACE|TRACK|GET|POST|HEAD)
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} /index\.php\?page=([\w-]+)&status_id=([\w-]+)&cf_store=([\w-]+) [NC]
RewriteRule ^ %1/%2/%3? [L,R]
RewriteCond %{THE_REQUEST} /index\.php\?page=([\w-]+)\s [NC]
RewriteRule ^ %1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?page=$1&status_id=$2&cf_store=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?page=$1 [L,QSA]

.htaccess redirect but keep url

I have the following code and I am trying to do something like that. Let's say I have an address - mydomain.com/server2/ when I type anything after slash for example mydomain.com/server2/whatever I want to load server2.php but address has to be the same
<IfModule mod_rewrite.c>
Options +FollowSymlinks
# Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
# remove index.php
RewriteRule ^index\.php/?$ / [L,R=301,NC]
# Hide File Extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
# Add 301 redirects to new extensionless file
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://www.mydomain.com/$1 [R=301,L]
# Add the trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://www.mydomain.com/$1/ [R=301,L]
</IfModule>
also I don't know if above code is 100% correct. Thanks for any help.
There are problems in your code and in certain URLs it will give you infinite looping. Replace your code with this:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=301,L,NE]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
# Add the trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
</IfModule>

Hide .php extension in ALL subdirectories with .htaccess

I am trying to hide the .php extensions on my page for all subdirectories with .htaccess.
Currently I have
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
It works perfectly fine with normal pages, however any page inside a folder gives me a 500 Internal Service Error. An example URL would be
http://www.motorcityline.com/portfolio/michigantinting
Which is actually /portfolio/michigantinting.php. The .php file does exist, I just don't have the .htaccess file configured properly.
Replace your .htaccess with this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# add www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
Try this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://www.%{SERVER_NAME}%{REQUEST_URI} [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /$1.php [L]

Resources