automatic rewrite URLs .htaccess - apache

I need to clean url for my web site.
The structure is like this :
example.com/home
inside the home folder I have:
index.php
ambiente.php
.htaccess
This is my .htaccess code:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+ambiente\.php\?ambiente=([^\s&]+) [NC]
RewriteRule ^ ambiente/%1? [R=301,L]
RewriteRule ^ambiente/([^/]+)/?$ ambiente.php?ambiente=$1 [L,QSA]
I would like the file did this:
example.com/home/ambiente.php?ambiente=living
example.com/home/ambiente/living
The problem is that the URLs are not automatically rewritten.
If I click on the ambiente.php?ambiente=living link in the index file, in the address bar I find example.com/home/ambiente.php?ambiente=living.
If I write in the address bar: http://example.com/home/ambiente/living, it works properly!
How can I solve this?

Your Rewritecondition matches /ambiente.php?ambiente=foobar but it does not match the orignal uri /home/ambiente.php?ambiente=foobar Replace your first rewriteRule with the following
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+home/ambiente\.php\?ambiente=([^\s&]+) [NC]
RewriteRule ^ /home/ambiente/%1? [R=301,L]

Related

.htaccess redirect to change parameter value

I would like to change the URL
/index.php?main_page=product_info&cPath=1&products_id=4
to
/index.php?main_page=product_info&cPath=141&products_id=302
I have tried
RewriteRule ^products_id=4$ products_id=302
but that doesn't seem to match.
My .htaccess file currently contains
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^products_id=302$ products_id=4 [L,R=301]
It would seem like I could use something like
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)products_id=4(&.*)?$
RewriteRule ^index\.php$ /index.php?%1products_id=302%3 [L,R=301]
but that doesn't work reliably. (If I click on a link, it doesn't work, but if I enter that URL in the address bar, it does.)
Try the following rule :
RewriteEngine on
RewriteCond %{THE_REQUEST} /index\.php\?main_page=([^&]+)&cPath=1&products_id=4\sHTTP [NC]
RewriteRule ^ /index.php?main_page=%1&cPath=141&products_id=302 [L,R]

Rewrite URL with .htaccess does not direct to the new URL

I use .htaccess to rewrite the URL
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^book/([^/]*)$ /book.php?book=$1 [NC,L]
This works great, and if I type http://www.domain.org/book/title1 in a browser's address bar it takes me there. But when I click the link on the index page it takes me to the old address http://www.domain.org/book.php?book=title1
It is because you don't have any rules that does the redirect. Have it this way:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+book\.php\?book=([^\s&]+) [NC]
RewriteRule ^ /book/%1? [R=302,L,NE]
RewriteRule ^book/([^/]+)/?$ book.php?book=$1 [NC,L,QSA]

Should be a simple 301 redirect

What I think should be simple is just not working.
I have switched domains
Old URL example:
digital.photorecommendations.com/recs/2015/01/big-zoom-field-review/
New URL example:
photorec.tv/2015/01/big-zoom-field-review/
Really just switching domain and dropping the recs folder from the URL
Using http://htaccess.madewithlove.be/ to test and the outputs the correct URL
Options +FollowSymlinks
RewriteEngine on
RewriteBase /recs
RewriteCond %{HTTP_HOST} !^www\.digital.photorecommendations\.com$ [NC]
RewriteRule ^recs(.*) http://photorec.tv/$1 [L,R=301]
When I place this in the htaccess file I get 404 errors on all the pages except the home page. The htaccess file is inside the /recs folder. I have also tried it in the root directory of digital.photorecommendations.com and I get no results at all.
Any suggestions?
Thanks!
You have wrongly used negation in RewriteCond and regex also needs a fix. Use this rule in /recs/.htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?digital\.photorecommendations\.com$ [NC]
RewriteRule ^(index\.php)?$ http://photorec.tv/ [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?digital\.photorecommendations\.com$ [NC]
RewriteRule ^(.+)$ http://photorec.tv/recs/$1 [L,R=301,NC,NE]

mod_rewrite keeps adding .html for files

I have a redirect situation where the site is part dynamic and part generated .html files.
For example, mysite.com/homepage and mysite.com/products/42 are actually static html files
Whereas other URLs are dynamically generated, like mysite.com/cart
Both mysite.com and www.mysite.com are pointing to the same place. However I want to redirect all of the traffic from mysite.com to www.mysite.com.
I'm so close but I'm running into an issue where Apache is adding .html to the end of my URLs for anything where a static .html file exists - which I don't want.
I want to redirect this:
http://mysite.com/products/42
To this:
http://www.mysite.com/products/42
But Apache is making it this, instead (because 42.html is an actual html file):
http://www.mysite.com/products/42.html
I don't want that - I want it to redirect to www.mysite.com/products/42
Here's what I started with:
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]
I tried making the parameters and the .html optional, but the .html is still getting added on the redirect:
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)?(\.html)?$ http://www.mysite.com/$1 [R=301,L]
What am I doing wrong? Really appreciate it :)
Here is the code you will need in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## first add www to your domain for and hide .html extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ http://www.mysite.com%1 [R=301,L]
## add www to your domain for other URIs without .html extension
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^ http://www.mysite.com%{REQUEST_URI} [R=301,L]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f [NC]
RewriteRule ^ %{REQUEST_URI}.html [L]
Maybe you should try looking at apache's mod_negotiation to get rid of the .html or any file extension?
Link: http://httpd.apache.org/docs/2.0/mod/mod_negotiation.html

htaccess redirect with variables

I realise there are similar questions but I'm trying to work out how I can rewrite the following URL:
http://www.myWEBaddress.com/?month=200904
to
http://www.myWEBaddress.com/my-page.php?month=200904
Regards
Add the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)month= [NC]
RewriteRule ^ my-page.php [L]
If you want to change the URL that the user sees in their browser address bar, change the last rule to
RewriteRule ^ my-page.php [L,R=301]