I am trying to find a way to rewrite my URLs so that IF the value of the variable p starts with a number the number is removed from the URL and the URL is rewritten with the value of p like so:
domain.com/index.php?p=23_Product&ms=0
rewritten to:
domain.com/Product&ms=0
My current htaccess file looks like this and everything works the way I want it to (except for the number removal part):
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?p=((?!(?:admin|superadmin|[^&]*?edit))[^\s&]+)(?:&(\S+))? [NC]
RewriteRule ^ /%1?%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?p=$1 [L,QSA]
I tried to make this modification but I get a redirect loop:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?p=([0-9]{0,99})(?:&(\S+))? [NC]
RewriteRule ^ /%2 [R=302,L,NE]
RewriteCond %{THE_REQUEST} /index\.php\?p=((?!(?:admin|superadmin|[^&]*?edit))[^\s&]+)(?:&(\S+))? [NC]
RewriteRule ^ /%1?%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[0-9]{0,99}(.+?)/?$ index.php?p=$2 [L,QSA]
Can you tell me what I am doing wrong?
Thanks
Related
I'm trying to beautify URL's for SEO from http://example.net/dashboard.php?action=news to http://example.net/news
And it's working with this code
RewriteEngine On
RewriteCond %{THE_REQUEST} \/+dashboard\.php\?([^\&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ dashboard.php?action=$1 [L,QSA]
But I want to edit more PHP files, for example, http://example.net/user.php?action=profile to http://example.net/profile
If I use this code again in the same .htaccess file
RewriteEngine On
RewriteCond %{THE_REQUEST} \/+user\.php\?([^\&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ user.php?action=$1 [L,QSA]
It won't work, only dashboard.php one is working properly.
Can you please help me with this?
Thank you in advance.
Your current Rewriterules are to general.
[^/]+
matches everything which has no "/", else its matching everything.
So basicly your first rule
RewriteRule ^([^/]+)/?$ dashboard.php?action=$1 [L,QSA]
matches everytime. profile/ will always redirected to dashboard.php?acton=profile.
Your .htaccess should look like:
RewriteEngine On
RewriteCond %{THE_REQUEST} \/+dashboard\.php\?([^\&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(news)/?$ dashboard.php?action=$1 [L,QSA]
RewriteCond %{THE_REQUEST} \/+user\.php\?([^\&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(profile)/?$ user.php?action=$1 [L,QSA]
As you can see i switched
[^/]+
with more explicit regex.
I want to change my URLS.
At the moment they look like this:
http://website.net/?page=news
http://website.net/?page=information&skin=217 (shows a mario skin with informations)
I want them to look like this:
http://website.net/news/
http://website.net/mario/
What I have tried so far is following:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^page=information&skin=217$
RewriteCond %{REQUEST_URI} ^\/$
RewriteRule .* http://website.net/Mario/? [R=301,L]
^ This Code works for changing the URL-Addresse, but I get a 404 Error: Page not found. Seems like this isnt working too well.
What did I miss and what do I have to change?
This also has to work with given parameters so: http://website.net/Mario/ should still know the parameters page=information, skin=217
Thanks
You can use this code in site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+\?page=information&skin=217\s [NC]
RewriteRule ^ /mario/? [R=301,L]
RewriteCond %{THE_REQUEST} \s/+\?page=([^\s&]+)\s [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
RewriteRule ^mario/?$ /?page=information&skin=217 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ /?page=$1 [L,QSA]
`RewriteCond %{THE_REQUEST} \s/+\?page=information&skin=([^\s&]+)\s [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ /?page=information&skin=$1 [L,QSA]
RewriteCond %{THE_REQUEST} \s/+\?page=information&resourcepack=([^\s&]+)\s [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ /?page=information&resourcepack=$1 [L,QSA]`
I have this now, works fine with all skins, but doesnt work for resourcepacks. Why?
Also tried to change %1 and $1 in the second blog to %2 and $2.
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.
i have a page with search form(fields: make,model,year,mileage,price)
on form submit url is:
http://www.example.com/buy-car.php?make=xxx&model=xxx&mileage=100;10000&year=2000;2015&price=20000;60000&sort=date;DESC
i want to rewrite to look like:
http://www.example.com/buy-car/xxx/xxx/100;10000/2000;2015/20000;60000/date;DESC
Note: query string may or may not contain all the variable
i tried multiple rules but still not getting what i want
RewriteCond %{THE_REQUEST} /buy-car\.php\?make=([^\s&]+)&model=([^\s&]+)&year=([^\s&]+)\&mileage=([^\s&]+)\&price=([^\s&]+)\s [NC]
RewriteRule ^ buy-car/%1/%2/%3/%4/%5? [R=302,L]
RewriteRule ^buy-car/([\w-]+)/([\w-]+)/([\^;a-zA-Z0-9_-]+)/([\^;a-zA-Z0-9_-]+)/([\^;0-9-]+)/?$ buy-car.php?make=$1&model=$2&year=$3&mileage=$4&price=$5 [L,QSA,NC]
RewriteCond %{THE_REQUEST} /buy-car\.php\?make=([^\s&]+)&model=([^\s&]+)&year=([^\s&]+)\&mileage=([^\s&]+)\s [NC]
RewriteRule ^ buy-car/%1/%2/%3? [R=302,L]
RewriteRule ^buy-car/([\w-]+)/([\w-]+)/([\^;a-zA-Z0-9_-]+)/([\^;a-zA-Z0-9_-]+)/?$ buy-car.php?make=$1&model=$2&year=$3&mileage=$4 [L,QSA,NC]
RewriteCond %{THE_REQUEST} /buy-car\.php\?make=([^\s&]+)&model=([^\s&]+)&year=([^\s&]+)\s [NC]
RewriteRule ^ buy-car/%1/%2/%3? [R=302,L]
RewriteRule ^buy-car/([\w-]+)/([\w-]+)/([\^;a-zA-Z0-9_-]+)/?$ buy-car.php?make=$1&model=$2&year=$3 [L,QSA,NC]
RewriteCond %{THE_REQUEST} /buy-car\.php\?make=([^\s&]+)&model=([^\s&]+)\s [NC]
RewriteRule ^ buy-car/%1/%2? [R=302,L]
RewriteRule ^buy-car/([\w-]+)/([\w-]+)/?$ buy-car.php?make=$1&model=$2 [L,QSA,NC]
RewriteCond %{THE_REQUEST} /buy-car\.php\?year=([^\s&]+)\s [NC]
RewriteRule ^ buy-car/%1? [R=302,L]
RewriteRule ^buy-car/([\^;0-9-]+)/?$ buy-car.php?year=$1 [L,QSA,NC]
RewriteCond %{THE_REQUEST} /buy-car\.php\?make=([^\s&]+)\s [NC]
RewriteRule ^ buy-car/%1? [R=302,L]
RewriteRule ^buy-car/([\w-]+)/?$ buy-car.php?make=$1 [L,QSA,NC]
what could be the solution.
thanks
Rather than maintaining so many rules like that and possibly more due to optional query parameters I strongly suggest using front controller .htaccess like this:
DirectryIndex index.php
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) index.php?uri=$1 [L,QSA]
Then use $_GET['uri'] in the php code to get requested URI and process them.
I had rewrite a URL from example.com/service.php?p_id=1&s_id=seo to example.com/1/seo/.
By The following rule:
this rule is for example.com/services.php?p_id=1&s_id=seo
RewriteEngine On
RewriteCond %{THE_REQUEST} /services\.php\?p_id=([^&\s]+)&s_id=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ services.php?p_id=$1&s_id=$2 [QSA,L]
Now the problem is that when i use the following rule for another url like example.com/about-us.php?p_id=1&s_id=2 and for example.com/it-training.php?p_id=1&s_id=2 then the url redirects as example.com/1/2/index.php but it should be as example.com/1/2 ,
formated rule for about-us.php is given bellow
RewriteEngine On
RewriteCond %{THE_REQUEST} /about-us\.php\?p_id=([^&\s]+)&s_id=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ about-us.php?p_id=$1&s_id=$2 [QSA,L]
In other case i have some url like example.com/packages.php, index.php, i also want to remove the .php from that kind of files