Rewrite URL from .htaccess - php

I have three four pages
page.php?s=defaultpage
page.php?p=defaultpage
blog.php?post=defaultpage
projects.php?view=defaultpage
I have this exisiting URL redirect code snippet. The fourth page is new, and I would like to write a condition for that to only show projects/defaultpage
How do I achieve this with the existing Rewrite condition.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Options -MultiViews
RewriteEngine On
RewriteBase /Renu/
RewriteCond %{THE_REQUEST} /page\.php\?([a-z])=([^&\s]+)
RewriteRule ^ %1/%2? [R=302,NE,L]
RewriteCond %{THE_REQUEST} /blog\.php\?post=([^&\s]+) [NC]
RewriteRule ^ blog/%1? [R=302,NE,L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^blog/([^/]+)/?$ blog.php?post=$1 [L,NC,QSA]
RewriteRule ^([a-z])/([^/.]+)/?$ page.php?$1=$2 [L,QSA]

Just add this line, before the last line:
RewriteRule ^projects/([^/]+)/?$ project.php?view=$1 [L,NC,QSA]

RewriteRule (.)/(.)/(.*)/$ /?view=$1&$2=$3
in php with get is :
example.com/?view=profile&nick=NickName
with .htaccess
example.com/profile/NickName/

Related

PHP htaccess rewrite rule to same page with different parameters

I'm using the following htaccess rules. The first one is working fine. The second rule is not working. The rule I have written for the second one is taking to the first one.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /Burjalif/
RewriteCond %{THE_REQUEST} /Products(?:\.php)?\?MC=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{THE_REQUEST} /Products(?:\.php)?\?MC=([^&]+)&SC=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# PHP hiding rule
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^([^/]+)$ Products.php?MC=$1 [L,NC,QSA]
RewriteRule ^([^/]+)/([^/]+)$ Products.php?MC=$1&SC=$2 [L,NC,QSA]
Please help me to fix this.
Change the order of the rules.
Its entering in the first rule every time.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /Burjalif/
RewriteCond %{THE_REQUEST} /Products(?:\.php)?\?MC=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{THE_REQUEST} /Products(?:\.php)?\?MC=([^&]+)&SC=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# PHP hiding rule
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/([^/]+)$ Products.php?MC=$1&SC=$2 [L,NC,QSA]
RewriteRule ^([^/]+)$ Products.php?MC=$1 [L,NC,QSA]
RewriteRule ^(.*)$ $1.php [L]
Change your first rule to this:
RewriteCond %{THE_REQUEST} /Products(?:\.php)?\?MC=([^\s&]+)\sHTTP [NC]
RewriteRule ^ %1? [R=302,NE,L]
This will ensure that you're matching only one parameter in query string.

how to remove question mark from two php URLs in .htaccess

I want to remove question mark from two URLs like this:
URL 1
http://localhost:8888/events/event.php?id=222
to
http://localhost:8888/events/event/222
URL 2
http://localhost:8888/membership/register.php?id=162
to
http://localhost:8888/membership/register/162
i'm use this code in .htaccess and just the 'event' page work:
Options -MultiViews
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /events/event(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /events/event/%1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^events/event/([^.]+?)/?$ events/event.php?id=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /membership/register(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /membership/register/%1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^membership/register/([^.]+?)/?$ membership/register.php?id=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
You can use:
Options -MultiViews
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /events/event(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /events/event/%1? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /membership/register(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /membership/register/%1? [R=302,L,NE]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# rewrite from pretty URL
RewriteRule ^events/event/([^.]+?)/?$ events/event.php?id=$1 [QSA,L,NC]
RewriteRule ^membership/register/([^.]+?)/?$ membership/register.php?id=$1 [QSA,L,NC]
# Rewrite with .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]

how to write multiple condition along with 301 redirect in .htaccess file

In my website i want to make 301 redirection rule and vanity url using .htaccess file and my code is
RewriteEngine On
RewriteCond %{HTTP_HOST} ^availany.com
RewriteRule (.*) http://www.availany.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC]
RewriteCond %{REQUEST_FILENAME} >""
RewriteRule ^([^\.]+)$ profile.php?user=$1 [L]
and in web page it shows 500internal error,in which position i done mistake do suggest me
Have your rules like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^availany\.com$ [NC]
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
RewriteRule ^([^.]+?)/?$ profile.php?user=$1 [L,QSA]

rewrite dynamic url after form submit

My link is
search?c=category&s=product
I have already removed the .php with htaccess, everything is working apart from when a user searches, the first page will show the link above, I want to show the link as below:
search/category/product
I tried the below code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^product/(.*)/(.*)/(.*) product.php?id=$1&c=$2&name=$3
RewriteRule ^search/(.*)/(.*)/(.*) search.php?c=$1&s=$2&page=$3
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{THE_REQUEST} \ /+search\?c=([^&\ ]+)&s=([^&]+)
RewriteRule ^ /search/%1/%2? [L,R]
What am I missing in the above?
Have rules this way:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /site/
RewriteCond %{THE_REQUEST} /search\?c=([^&\s]+)&s=([^&\s]+) [NC]
RewriteRule ^ search/%1/%2? [L,R]
RewriteRule ^product/([^/]+)/([^/]+)/([^/]+)/?$ product.php?id=$1&c=$2&name=$3 [L,QSA]
RewriteRule ^search/([^/]+)/([^/]+)/([^/]+)/?$ search.php?c=$1&s=$2&page=$3 [L,QSA]
RewriteRule ^search/([^/]+)/([^/]+)/?$ search.php?c=$1&s=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

Rule for "/username" in HTACCESS

I want to implement a RewriteRule in my .htaccess to enable the following URL structure:
http://example.com/([a-zA-Z\.]*) -> user.php?username=$1
I tried it like this:
RewriteRule ^([a-zA-Z\.]*)$ user.php?username=$1 [L,QSA]
If I go to localhost/victorbarbu, it will redirect to user.php?username=victorbarbu, but if I go to storage/, it will still go to user.php. What can I do to avoid this type of redirection?
This is my current .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{THE_REQUEST} \ /+subdir/
RewriteRule ^ - [L]
Try this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteRule ^([a-zA-Z.]*)$ user.php?username=$1 [L,QSA]

Categories