How to define base URL in .htaccess - php

I am redirecting
https://www.example.com/inter.php?pid=<a number>&title=<the page title>
to
https://www.example.com/<a number>/<the-page-title>
Now I want to move the website to a sub folder so the redirection will be
https://www,example.com/folder/inter.php?pid=<a number>&title=<the page title>
to
https://www.example.com/folder/<a number>/<the-page-title>
I am using the below .htaccess code
RewriteEngine On
RewriteCond %{THE_REQUEST} /inter\.php\?pid=([^&\s]+)&title=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,NE,L]
RewriteRule ^(\S+)\s+(.*)$ $1-$2 [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^(\S+)$ /$1 [NE,R=302,L]
RewriteRule ^([^/]+)/([^/]+)/?$ inter.php?pid=$1&title=$2 [L,QSA]
Please Suggest.
thanks!

If you are using .htaccess in root directory you can add /foldername
RewriteCond %{THE_REQUEST} /folder/inter\.php\?pid=([^&\s]+)&title=([^\s&]+) [NC]
RewriteRule ^ /foldername/%1/%2? [L,R]
To both rules
RewriteRule ^folder/([^/]+)/([^/]+)/?$ inter.php?pid=$1&title=$2 [L,QSA]
Please try above rules it I didn't tried it for now.

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.

Removing a word from our urls

We have the following file structure in our ftp account.
/index.php
/views/account/users/index.php
/views/account/clients/index.php
/views/profile/index.php
/views/settings/index.php
....
Which means that the urls is as follows:
www.thesite.com/
www.thesite.com/views/account/users/
www.thesite.com/views/account/clients/
www.thesite.com/views/profile/
www.thesite.com/views/settings/
...
What we need, is to remove views from all possible variations, so that we can link to, or enter the following url in the browser's address bar.
www.thesite.com/account/users/
Any help would be greatly appreciated.
Try these rules in your site root .htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+views/(\S*)\s [NC]
RewriteRule ^ /%1 [NE,L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!views/).+)$ views/$1 [NC,L]
You can use the following Rules in Root/.htaccess :
RewriteEngine on
#/foo to /views/foo
RewriteCond %{THE_REQUEST} /views/([^/.\s]+) [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteRule ^([^/.]+)/?$ /views/$1 [NC,L]
#Rewrite /foo/bar to /views/foo/bar
RewriteCond %{THE_REQUEST} /views/([^/]+)/([^/.\s]+) [NC]
RewriteRule ^ /%1/%2 [NC,L,R]
RewriteRule ^([^/]+)/([^/.]+)/?$ /views/$1/$2 [NC,

htaccess rewrite links and duplicate it

I have this htaccess code
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^einfogarden.com$
RewriteRule (.*) http://www.einfogarden.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/+single\.php\?title=([^\s&]+) [NC]
RewriteRule ^ %1/? [R=302,L,NE]
## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=302]
# convert %20 to -
RewriteRule "^(\S*) +(\S* .*)$" $1-$2 [L,NE]
RewriteRule "^(\S*) (\S*)$" $1-$2 [L,R=302,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ single.php?title=$1 [NE,L,QSA]
which should remove single.php?title= from url and replace the 20% by slash and it work corrctly.
but i have 2 problem 1. it stop the css in my website 2. if you try to click any link (except) the home page it will give you something like this http://www.einfogarden.com/%D9%81%D9%88%D8%A7%D8%A6%D8%AF-%D8%A7%D9%84%D8%AC%D8%B1%D8%AC%D9%8A%D8%B1/single.php?title=%D9%81%D9%88%D8%A7%D8%A6%D8%AF%20%D8%A7%D9%84%D8%B1%D9%85%D8%A7%D9%86%20%D8%A7%D9%84%D8%B1%D8%A7%D8%A6%D8%B9%D8%A9
it duplicate the link
You are asking in the .htaccess the url of your current page.
So yourwebsite.com/home will be yourwebsite.com/home/home
I edited your code a little. using this will do fine.
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule ^(.*?)$ $1 [L]
RewriteCond %{REQUEST_URI} !^/cache
RewriteCond %{REQUEST_URI} !^/images
RewriteCond %{REQUEST_URI} !.*\.(css|jpg|gif|zip|js)
RewriteRule ^(.*)/(.*)/?$ index.php?page1=$1&page2=$2 [L]
RewriteRule ^(.*) index.php?page1=$1 [L]
#RewriteRule ^(.*)/?$ http://www.google.com/ [R]
This is a easy way the use SEO url's
The RewriteRule is for creating a new var to your url.

.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.

Categories