htaccess redirect with variables - apache

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]

Related

Mod_rewrite redirect not working as intended (working in htaccess checker)

I'm trying to redirect users accessing https://www.mywebsite.com/index.php?p=home to https://www.mywebsite.com/. I have already added the code below to my public_html's .htaccess file and have tested it on an htaccess checker.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteCond %{QUERY_STRING} ^p=home$ [NC]
RewriteRule .* https://%{HTTP_HOST}/ [R=301,L,QSD]
Based on my code, do you have any idea why the redirect is not working even though the htaccess checker says it should be working.
You could try following Rules in your htaccess rules file. Please don't trust htaccess checker sites they don't give correct results. Have your htaccess file with following rules and make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
##Redirection external rules here.
RewriteCond %{THE_REQUEST} \s/index\.php\?p=home\s [NC]
RewriteRule ^ https://%{HTTP_HOST} [R=301,L]
##Rewrite internal rule here.
RewriteRule ^/?$ index.php?p=home [L]

automatic rewrite URLs .htaccess

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]

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]

Redirect in .htaccess if file exists (cache)

I do not understand anything .htaccess and I need a redirection to cache.
Currently use the following rule:
RewriteRule ^movie/(.*)/(.*)/(.*)$ app.php?view=movie&theatre=$1&ordem=$2&movie=$3 [L,QSA]
That is, the requested URL
movie/jardins/0/robocop
redirects to
app.php?view=movie&theatre={jardins}&ordem={0}&movie={robocop}
But I want to redirect api/cache/html/{jardins}/{0}/{robocop}.html if file exists.
Recalling that {jardins} and {0} are directories.
How to do this? :(
You can have:
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/app/cache/html/$1/$2/$3\.html -f [NC]
RewriteRule ^movie/(.*)/(.*)/(.*)$ app/cache/html/$1/$2/$3.html [L,NC]
RewriteRule ^movie/(.*)/(.*)/(.*)$ app.php?view=movie&theatre=$1&ordem=$2&movie=$3 [L,QSA]

.htaccess rewrite to simultaneously change domain and remove path

My URL structure is currently as follows:
http://domain.com/folder/filename (CURRENT)
I want to change this so that I can use the following URL instead:
http://sub.domain.com/filename (NEW)
So accessing the CURRENT or the NEW url, should load the file located at the CURRENT url, but show the NEW url in the address bar. It should only apply to the "/folder/" path.
sub.domain.com is a mirror of domain.com, ie. they share the same file system and root directory.
This is what I have so far:
Options +FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/folder/?(.*)$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
This is working, but is missing the rule to remove the "/folder/" from the path. I've tried combining multiple RewriteRule's with no luck. Any ideas? Thanks.
UPDATE: Thanks again #Gerben - I understand what your rules are doing now, but the second one isn't working for me. I suspect because it's conflicting with some other rewrite rules, in particular those of WordPress, which are lower down in my .htaccess file:
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Because of this the page ends up in a redirect loop, ie (from Chrome):
"The webpage at http://sub.domain.com/folder/index.php has resulted in too many redirects." - while the url I was originally trying to access was, for example, http://sub.domain.com/page
Any ideas?
Try:
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^(folder/)?(.*)$ http://sub.domain.com/$2 [R=301,L]
This will redirect everything to sub.domain.com, and remove the /folder part of the URI if it is there. If not, it redirects and leaves the URI untouched.
RewriteCond %{THE_REQUEST} /folder/
RewriteRule ^folder/(.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ folder/$1 [L]
# WordPress rules here
edit the second R=301 should not have been there
But this won't work, as wordpress has no way of knowing you want folder. You could add the Proxy flag to the rewrite, but then you need to change the rule above to not redirect on this internal proxy request.