htaccess: RewriteRule not working - .htaccess

I'm trying to clean my url from this:
https://www.sobarroso.pt/stand-carros-usados/?brand=2&model=&category=&fuel=&kms=&price=
into this:
https://www.sobarroso.pt/stand-carros-usados/alfa-romeo
but when i creat the rule in .htaccess it just doesn´t work.
Here it´s a snippet of my .htaccess
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
And the code i was trying to add was this:
RewriteRule ^stand-carros-usados/alfa-romeo$ /stand-carros-usados/?brand=2&model=&category=&fuel=&kms=&price= [L]
What am i doing wrong? I'm just starting to play with .htaccess so i need you help guys.
Thanks
EDIT:
hey guys, i keep playing with the codes and feel im almost there. this time i used this one
RewriteCond %{REQUEST_URI} ^/stand-carros-usados [NC]
RewriteCond %{QUERY_STRING} ^brand=(.*)&model=(.*)&category=(.*)&fuel=(.*)&kms=(.*)&price=(.*)
RewriteRule (.*) https://www.sobarroso.pt/stand-carros-usados/alfa-romeo? [L]
and it worked. The url is clean but gives me a 404 page. Do you know why?

Related

How do I redirect dynamic URLs with parameters?

The old URLs are like this:
http://mywebsite.com/index.php?mode=thread&id=284607
The new URLs would be:
http://mywebsite.com/threads/284607
As you can see, I want to grab the ID # from the old dynamic URLs and point them to the new pretty URLs.
I've tried finding the solution here and elsewhere, but keep having problems removing the "mode=thread" part from the redirect.
Thanks for your help!
Update: Here's some other code that is already in the .htaccess file.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
After a lot of Googling, I found this to work:
RewriteCond %{QUERY_STRING} (^|\?)mode=thread&id=([0-9]+)($|&)
RewriteRule ^index\.php$ http://mywebsite.com/threads/%2/? [R=301,L]
In your .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^mode=thread
RewriteCond %{QUERY_STRING} &id=([0-9]+)
RewriteRule ^index\.php$ /threads/%1? [L,R=301]

URL re-writing using .htaccess

I have a URL:
domain.com/abc/hotel_detail.php?id=2
And I want to do a URL re-write to make it look like this :
domain.com/abc/hotel_detail/2
My current .htaccess looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{QUERY_STRING} ^id=20$
RewriteRule ^abc/hotel_detail/([0-9]+)$ ^abc/hotel_detail.php?id=$1 [L]
The first part contains code for removing the extension(.php) from pages.
When I try to open this link domain.com/abc/hotel_detail/2 , it gives me object not found error.
Can Someone please tell me whats wrong with the code?
Remove this line:
RewriteCond %{QUERY_STRING} ^id=20$
Changing your file to:
RewriteEngine On
RewriteRule ^abc/hotel_detail/([0-9]+)$ ^abc/hotel_detail.php?id=$1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,R]
should work. I removed RewriteCond %{QUERY_STRING} ^id=20
Your first rewritecond and rule was overwriting the second one, switching them around should fix it.
I think this should work
RewriteRule ^abc/hotel_detail/([0-9]*)$ abc/hotel_detail.php?id=$1 [L]
([0-9]*) miltiply sign.
and the ^ for the real link

htaccess redirect not working properly

I am trying to accomplish the following but my code is not working
1) I want to do either non-www to www or www to non-www
2) desktop users should be redirected to example.com/homepage
3) remove or hide /homepage from url
4) redirect mobile users to example.com/m
here is my code, it's not working, too many problems to list, but off the top of my head, I can think of 2 problems that I encountered: "cannot open page because too many redirects occurred" and mobile users are taken to /homepage instead of /m
# remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
RewriteEngine On
RewriteRule ^$ /homepage [L]
RewriteEngine On
RewriteRule ^$ homepage/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ homepage/$1
RewriteCond %{HTTP_USER_AGENT} ^.*(ip(ad|od|hone)|blackberry|iemobile|android).*$ [NC]
RewriteCond %{REQUEST_URI} !^/m/.*
RewriteRule ^.*$ http://example.com/m [R=301,L]
Please help me, I am so frustrated, what am I supposed to change about this code to get it to work?
Unfortunately im no genious with regex or redirects in htacces but I have gotten similar code to work myself.
It does sound like your code is getting stuck in a redirect loop and the browser is stopping it from continuing.
I know thats not a complete answer but Hopefully that helps you make more sense of the code atleast a little.
Hopefully someone else can chime in otherwise i can dig up the code that is working when im on the computer and hopefully give you a better response.
-Sent from my cell
The www/non-www is fine.
For the homepage stuff, you need to do two things: make sure what you're rewriting into the homepage directory actually exists (otherwise you get a 500 server error) and exclude mobile user agents.
So this stuff:
RewriteEngine On
RewriteRule ^$ /homepage [L]
RewriteEngine On
RewriteRule ^$ homepage/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ homepage/$1
Needs to look like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_USER_AGENT} !^.*(ip(ad|od|hone)|blackberry|iemobile|android).*$ [NC]
RewriteCond %{DOCUMENT_ROOT}/homepage%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/homepage%{REQUEST_URI} -d [OR]
RewriteRule ^(.*)$ homepage/$1 [L]

Htaccess how to properly route to the correct directory?

I have a folder structure on my server like so:
.htaccess
index.php
[website1]
[placeholder]
index.php
[website2]
[website3]
All domains web1.com, web2.com and web3.com link to this directory. I like the htaccess to route them to the proper directory. So web1.com goes to website1, web2.com goes to website2, etc.
However I like the URL to be something like http://www.web1.com/ and it should show the content in website1.
Now I have the following code:
RewriteCond %{HTTP_HOST} ^(web1.com)$ [NC]
RewriteRule ^(.*)$ http://www.web1.com/$1
RewriteCond %{HTTP_HOST} ^(www.web1.com)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /website1/$1
RewriteCond %{HTTP_HOST} ^(www.web1.com)$ [NC]
RewriteRule ^(/?|index.php)$ website1/index.php [L]
This works, however if you go to http://www.web1.com/placeholder it will display the following in the URL:
http://www.web1.com/website1/placeholder/
Of course it's preferred to see the URL as http://www.web1.com/placeholder. For the record http://www.web1.com/placeholder/ does show the proper page.
How can I fix this? It's driving me nuts.
EDIT (ANSWER):
RewriteCond %{HTTP_HOST} ^(www.web1.com)$ [NC]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^website1/(.*)([^/])$ http://%{HTTP_HOST}/$1$2/
Not sure if this is the proper way to do it, but it seems to work.

Force indexhibit to use url with www

i was trying to make my indexhibit installation to use nice urls + domain redirect to make it allways using 'www' in page URL but i've failed. Nice urls works fine but i can't make this www redirect work.
Here is my original htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteRule ^(.+) index.php
I was trying to add those lines
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]
before RewriteCond %{REQUEST_FILENAME} -f [OR], after RewriteRule ^(.+) index.php and in any possible place but it allways breaks the page. Can you please advise me how to make it properly ? Thanks in advance :)

Resources