Unable to rewrite the url with multiple condition - php

i need following url to be changed as
http://example.com/Automobile/automotive_listing_subcategory?id=1&title=car-suv also need to rewrite urls such as: http://example.com/Automobile/automotive_listing_category?aid=1&title=car-suv
etc. these all pages under same folder. i had the following code.first one works.but if try to rewrite according to second condition it doesnt work. Anybody please help me.Am not an expert in this area. please correct me if there is any errors.
Options +FollowSymLinks -MultiViews RewriteEngine On RewriteBase /Automobile/
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?id=([^&\s]+)\&title= ([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3? [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?id=$2&title=$3[QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?aid=([^&]+)&title=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3? [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)(/[^/]+)?/?$ $1.php?aid=$2 [L,QSA]

you can read https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/ and what you need are regular expressions (regexes)
regexes are are chapter for themselves and very tricky to understand sometimes
they can also be used with linux commands like grep, sed,...
https://en.wikipedia.org/wiki/Regular_expression
the Apache server has a (url) rewrite engine: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Related

Rewrite Rule in htaccess convert query string into slashes php

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]

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.

Rewrite in index.php

I have a problem with rewriting hrefs in my website. First of all I managed to remove all .php extensions from na URLs, then I added rewriting all mydomain.com/img?id=X to mydomain.com/img/X, but I have been struggling with one more thing for some time and I would appreciate any help. My website consists of many pages and the link to page number X is mydomain.com/?page=X (e.g mydomain/?page=3), but I want to rewrite that URLs to mydomain.com/page/X
Here is my .htaccess:
RewriteEngine on
RewriteBase /
<something not important>
RewriteCond %{HTTP_HOST}//s%{HTTPS} ^www\.(.*)//((s)on|s.*)$ [NC]
RewriteRule ^ http%3://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^img/([0-9]+)/?$ img.php?id=$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Right below this rule:
RewriteCond %{HTTP_HOST}//s%{HTTPS} ^www\.(.*)//((s)on|s.*)$ [NC]
RewriteRule ^ http%3://%1%{REQUEST_URI} [L,R=301]
try adding:
RewriteCond %{THE_REQUEST} \ /+\?page=([0-9]+)
RewriteRule ^ /page/%1? [L,R]
RewriteRule ^page/([0-9]+)/?$ /?page=$1 [L]

.htaccess file not working after adding custom url

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.

Good profile urls using .htaccess

I've three url types which are as follows:
http://www.mywebsite.com/myprofile.php?user_username=username
http://www.mywebsite.com/input.php?user_username=username
http://www.mywebsite.com/users.php?user_username=username
Currently, users have to type the whole address like (http://www.mywebsite.com/myprofile.php?user_username=username) to go to their profiles and the same with input and users.
What I want is if a user types http://www.mywebsite.com/profile/username, he would be automatically redirected to http://www.mywebsite.com/myprofile.php?user_username=username.
When a user types http://www.mywebsite.com/input/username, he would be redirected to
http://www.mywebsite.com/input.php?user_username=username.
When a user types http://www.mywebsite.com/username, he would be redirected to http://www.mywebsite.com/users.php?user_username=username.
I know that this can only be achieved through .htaccess. However, I've searched but with no fruitful result.
Any help please!
Update
I think that the following code is close to being correct. However, I'm not getting the correct result and the css is also getting messed a lot. Individually they are working but together they are messing up. Any help please.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /myprofile\.php\?user_username=(.*)\ HTTP
RewriteRule ^ /myprofile/%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user_username=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ myprofile.php?user_username=$1 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /user\.php\?user_username=(.*)\ HTTP
RewriteRule ^ //%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user_username=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ user.php?user_username=$1 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /input\.php\?user_username=(.*)\ HTTP
RewriteRule ^ /input/%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user_username=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ input.php?user_username=$1 [L]
Have it this way:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+input\.php\?user_username=([^\s&]+) [NC]
RewriteRule ^ /input/%1? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+myprofile\.php\?user_username=([^\s&]+) [NC]
RewriteRule ^ /profile/%1? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+user\.php\?user_username=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^input/([^/]+)/?$ input.php?user_username=$1 [NC,QSA,L]
RewriteRule ^profile/([^/]+)/?$ myprofile.php?user_username=$1 [NC,QSA,L]
RewriteRule ^([^/]+)/?$ users.php?user_username=$1 [NC,QSA,L]
Add these to the .htaccess file in your DOCUMENT_ROOT
RewriteEngine On
RewriteRule ^([^/]+)/?$ users.php?user_username=$1 [DPI,L]
RewriteRule ^input/([^/]+)/?$ input.php?user_username=$1 [DPI,L]
RewriteRule ^profile/([^/]+)/?$ myprofile.php?user_username=$1 [DPI,L]
Tested in Apache 2.2 and 2.4 :)
This assumes that mod_rewrite is both installed and activated for htaccess files.
If you are not sure, to check if mod_rewrite is installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

Categories