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.
Related
OK I've tried multiple solutions but still can't achieve it. so what I need to do is
mydomain.com/search?q=product&l=london
to
mydomain.com/search/product/london
Basically I'm using rewrite rules to hide .php extension by following this code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-
RewriteRule ^([^\.]+)$ $1.php [NC,L]
So now, search.php converts into search and when user searches. it redirects to trailing slash instead of query string
Here what I've used so far
RewriteCond %{THE_REQUEST} /search/?q=([^&]+)&l=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [NC,L,R]
#skip the rule if the request is for dir
RewriteCond %{REQUEST_FILENAME} !-d
#skip the rule if the request is for file
RewriteCond %{REQUEST_FILENAME} !-f
#rewrite any other request to "/packagemenu.php?"
RewriteRule ^([^/]+)/([^/]+)/?$ /search?q=$1&l=$2 [NC,L]
AND
RewriteBase /search/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)&l=([^\s&]+) [NC]
RewriteRule ^ search/%1/%2/? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)\s [NC]
RewriteRule ^ search/%1/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# internal forward from pretty URL to actual one
RewriteRule ^search/([^/]+)/?$ search?q=$1 [NC,L,QSA]
RewriteRule ^search/([^/]+)/([^/]+)/?$ search?q=$1&l=$2 [NC,L,QSA]
But both of them doesn't work. I don't have so much knowledge about so please help me out.
Have your site root .htaccess as this:
Options -MultiViews
RewriteEngine On
RewriteBase /jobportal/
# To externally redirect /search.php?q=product&l=london to /search/query/london/
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)&l=([^\s&]+)\s [NC]
RewriteRule ^ search/%1/%2/? [R=301,L,NE]
# To externally redirect /search.php?q=product to /search/query/
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)\s [NC]
RewriteRule ^ search/%1/? [R=301,L,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC]
RewriteRule ^ %1 [R=301,NE,L]
# ignore all rules below for real files/directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# internal forward from /search/product/ to search.php?q=product
RewriteRule ^search/([^/]+)/?$ search.php?q=$1 [NC,L,QSA]
# internal forward from /search/product/london/ to search.php?q=product&l=london
RewriteRule ^search/([^/]+)/([^/]+)/?$ search.php?q=$1&l=$2 [NC,L,QSA]
# internal forward from /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
You can achieve the URL mydomain.com/search/product/london by using the following rule in your .htaccess.
RewriteEngine On
RewriteRule ^search/([^/]*)/([^/]*)$ /search?q=$1&l=$2 [L]
Just make sure you clear your cache before testing this.
Try as below,
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/search$
RewriteCond %{QUERY_STRING} ^q=([^\/]+)&l=([^\/]+)$
RewriteRule ^ search/%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Rewriterule ^search/([^\/]+)/([^\/]+)$ search.php?q=$1&l=$2 [QSA,L]
Hi I'm using the following htaccess.
RewriteEngine On
RewriteBase /JustSnacksNew/
RewriteCond %{THE_REQUEST} /Products\.php\?category=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{THE_REQUEST} /Products\?category=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteRule ^([A-Z,0-9,a-z-]+)$ Products.php?category=$1 [L,NC,QSA]
RewriteCond %{THE_REQUEST} /ViewProduct\.php\?category=([^\s&]+)&product=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{THE_REQUEST} /ViewProduct\?category=([^\s&]+)&product=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteRule ^([A-Z,0-9,a-z-]+)$ ViewProduct.php?category=([^\s&]+)&product=$1 [L,NC,QSA]
# PHP hiding rule
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
In the first rule i'm passing one variable.It's working fine.But in the second rule i'm passing two variables.But it is redirecting to the first rule.
Please help me to fix this.
First url i'm passing is : ./Products.php?category=catname
Second url passing is : ./ViewProduct.php?category=catname&product=product
You can use:
RewriteEngine On
RewriteBase /JustSnacksNew/
RewriteCond %{THE_REQUEST} /ViewProduct(?:\.php)?\?category=([^&]+)&product=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L]
RewriteCond %{THE_REQUEST} /Products(?:\.php)?\?category=([^\s&]+) [NC]
RewriteRule ^ %1? [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 ^([A-Z,0-9-]+)/?$ Products.php?category=$1 [L,NC,QSA]
RewriteRule ^([A-Z,0-9-]+)/([A-Z,0-9-]+)/?$ ViewProduct.php?category=$1&product=$2 [L,NC,QSA]
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/
I am trying to just update my .htaccess file so that I get nicer looking url's (without the file extenstion of .php). The file already has a redirect in it as I have both domain names.
Here is the original code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^darrenmorton\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.darrenmorton\.com$
RewriteRule ^/?$ "http\:\/\/darrenmorton\.co\.uk\/" [R=301,L]
And here is my updated code that is not working
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^darrenmorton\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.darrenmorton\.com$
RewriteRule ^/?$ "http\:\/\/darrenmorton\.co\.uk\/" [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
Not
RewriteCond %{REQUEST_FILENAME} \.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^contact-darren-morton.php$ contact-darren-morton.php [L]
Not sure what is the problem as I am new to .htaccess files!
You can use:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?darrenmorton\.com$ [NC]
RewriteRule ^ http://darrenmorton.co.uk}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
You are redirecting everything with that statement:
RewriteRule ^/?$ "http\:\/\/darrenmorton\.co\.uk\/" [R=301,L]
Nothing after that will ever work, because it is the last rule to be checked if it matches (and it matches always). Defined by you with the [L].
So at least you need to change the order of your rules. If you want the rules to only work on your .co.uk domain you have other options, too.
I'm using this code to remove the .php from the end of my URL's making them more SEF.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(.+)\.php[^\s]* [NC]
RewriteRule ^ /%1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [QSA,NC,L]
This is generic and works perfectly for every UR but I would like to know how to create an exception for one and only one URL, ie:
http://www.domain.com/services to http://www.domain.com/services.php
without influencing all the others. Is this possible?
I've tried a simples Redirect but entered a loop.
! EDITED !
I failed to mention I have also these rules:
Add www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Remove index (the extension .php from index has been trimmed already)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index/?([^\ \?]*) [NC]
RewriteRule ^ %1/%2 [R=301,L]
You should :
add a RewriteCond, to disabled services.php => services
add another rule, to enforce services => services.php, as the first rule doesn't prevent user to manually use services url
Full .htaccess
# domain.tld > www.domain.tld (visible)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# services > services.php (visible)
RewriteRule ^services/?$ services.php [R=301,QSA,L]
# filename > filename.php (transparent)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [QSA,NC,L]
# filename.php > filename (visible, exception for services.php)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(.+)\.php[^\s]* [NC]
RewriteCond %{REQUEST_FILENAME} !services\.php
RewriteRule ^ /%1 [R=301,NE,L]
# index > /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index/?([^\ \?]*) [NC]
RewriteRule ^ %1/%2 [R=301,L]
I would believe just a small change in your .php removal rule should do the job:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(.+?)\.php[\s?] [NC]
RewriteRule !^services\.php$ /%1 [R=301,NE,NC,L]
Can you try this,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You can add one rule for each php file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^services /services.php [L]