REWRITE URL and keep QUERY STRING in .htaccess - php

Here is my .htaccess code...
ErrorDocument 404 /404.html
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !\.php$
RewriteCond %{REQUEST_URI} !\.css$
RewriteCond %{REQUEST_URI} !\.js$
RewriteCond %{REQUEST_URI} !\.png$
RewriteCond %{REQUEST_URI} !\.mp3$
RewriteCond %{REQUEST_URI} !\.jpg$
RewriteCond %{REQUEST_URI} !\.html$
RewriteCond %{REQUEST_URI} !\.xml$
RewriteCond %{REQUEST_URI} !\.txt$
RewriteCond %{REQUEST_URI} !\.gif$
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com" [R=301,L]
I want to have: example.com/buy?r=abc123 appear like: example.com/release/abc123 or even like: example.com/abc123
I cannot $_GET the contents of r in buy.php once the url has been redirected.
I hope someone can help :)

If you want clean URLs then you can simply use:
RewriteRule ^([^/]*)$ /buy?r=$1 [L]
to get example.com/abc123
Or you can use:
RewriteRule ^release/([^/]*)$ /buy?r=$1 [L]
to get example.com/release/abc123.
Just make sure you clear your cache before testing this.

Related

.htaccess rewrite except some request_uri

I have a .htaccess file but it's not working as expected.
Redirect /some/dir http://www.example.com/someother/dir/29
<IfModule mod_rewrite.c>
RewriteEngine on
# HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !=/buy/confirm
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteCond %{REQUEST_URI} !=/some/dir
RewriteRule ^(.*)$ index.php?issue=$1 [L,QSA]
</IfModule>
I've been checking this with http://htaccess.mwl.be/ and I don't understand a line when I enter "http://www.example.com/buy/confirm":
RewriteCond %{REQUEST_URI} !=/buy/confirm This condition was met.
What exactly contains %{REQUEST_URI} to met the condition != /buy/confirm?? I've tried both /buy/confirm and buy/confirm and nothing.
I don't want to redirect to HTTPS if the url is buy/confirm.
Thanks!
You can use:
Redirect /some/dir http://www.example.com/someother/dir/29
<IfModule mod_rewrite.c>
RewriteEngine on
# HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/buy/confirm
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/favicon.ico
RewriteCond %{REQUEST_URI} !^/some/dir
RewriteRule ^(.*)$ index.php?issue=$1 [L,QSA]
</IfModule>
Try this:
RewriteEngine On
#redirect everything except /buy/confirm to https
RewriteCond %{HTTPS} =off
RewriteCond %{REQUEST_URI} !^\/buy\/confirm\/
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
#redirect /buy/confirm to http if it is accesed by https
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} \/buy\/confirm\/
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]

How to set up Apache mod_rewrite redirect rules?

I am not quite familiar with Apache settings. I need to make website loading sub-directory content except one page.
Currently got a website and need to make all calls to http://www.domain.com & http://domain.com load contents from http://www.domain.com/subfolder (but looks like http://www.domain.com)
Only except the http://www.domain.com/checkout page, this one page should redirect to https://www.domain.com/checkout for secure checkout
The current mod_rewrite shown as below:
RewriteEngine on
RewriteRule ^$ domain/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/domain%{REQUEST_URI} -f
RewriteRule .* domain/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* domain/index.php?q=$0 [QSA]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://domain.com.au/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://domain.com.au$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain.com.au/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain.com.au$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ http://www.domain.com.au [R,NC]
Open the file named .htaccess in the root of your webserver and add following lines of code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^checkout/(.*)$ https://www.yourdomain.com/checkout/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_URI} !^/checkout/
RewriteRule ^(.*)$ /subfolder/$1 [NE,L,QSA]
rewrite for your complete .htaccess-file (check if this works, then I'll delete the previous code):
RewriteRule ^$ subfolder/index.php [QSA,L]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ http://www.yourdomain.com [NC]
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^checkout/(.*)$ https://www.yourdomain.com/checkout/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^subfolder/(.*) /subfolder/index.php?q=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_URI} !^/checkout/
RewriteRule ^(.*)$ /subfolder/$1 [NE,L,QSA]

htaccess not working for www and non-www

i'm trying to redirect www.site.ru and site.ru to www.site.ru/ru_RU. But i can't make it work.
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.ru$ [NC]
RewriteRule ^(.*)$ http://www.site.ru/ru_RU [L,R]
RewriteCond %{HTTP_HOST} ^www\.site\.ru$ [NC]
RewriteRule ^(.*)$ http://www.site.ru/ru_RU [L,R]
RewriteCond %{REQUEST_URI} ^/news
RewriteRule (.*) /news [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
It's not redirecting WWW version. Can someone tell me how to make that request. By the way, sometimes i come to situation, where in firefox it's working, but in IE it's not.
Try this code:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?site\.ru$ [NC]
RewriteRule ^$ http://www.site.ru/ru_RU [L,R]
## WHAT IS THIS RULE DOING??
# RewriteCond %{REQUEST_URI} ^/news
# RewriteRule ^ /news [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
</IfModule>
btw I had to comment out suspicious looking 2nd rule for /news. If you can explain what you're trying to do with this I can suggest you how to fix it.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ru_RU [R=permanent,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/ru_RU [R=301,L]

Using .htaccess to rewrite dynamic subdomains

I'm currently using .htaccess to rewrite on my website to create dynamic subdomains. I also use it to remove the .php extension on all pages. However, once inside the subdomain, it tries to redirect again. For example, if you went to https://admin.example.com/test, it would actually be accessing https://example.com/clients/admin/test.php. I keeping getting various 404 errors using the following .htaccess file:
Options +MultiViews
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^subdomains/(.*)/(.*) http://$1.example.com/$2 [r=301,nc]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ clients/%2/$1 [QSA,L]
How can I keep this from redirecting to https://admin.example.com/clients/admin/test.php?
First, you probably want to turn off multiviews, that's going to mess with the whole "removing the php extension" thing
Than, you want this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
To be after your redirects, and you want to be doing the redirect status check on the actual redirects:
Options -Multiviews + FollowSymLinks
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^subdomains/(.*)/(.*) http://$1.example.com/$2 [r=301,nc]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_URI} !^/clients/
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ clients/%2/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

How do I redirect to a page of my site taking the trailing slash as the language parameter using .htaccess

I have this rule which is not redirecting properly:
#redirect from www.mysite.com/page-NAME/es to www.mysite.com/public/index.php?page=NAME/es
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule page-(.*)/es$ public/index.php?page=$1&lang=es [L]
Internally the parameter page is not getting received so the site shows the home page instead. I am not sure if I have this rule wrong or if it is just conflicting with the other rules which are working perfectly, or if it is an order problem.
All help is greatly appreciated.
Thanks in advance.
Below is the full htaccess file on the root of my site, in case you want to take a look at it.
Options +FollowSymlinks
RewriteEngine on
DirectoryIndex index.php
#Adds www to the domain
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.mysite.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
#redirect from www.mysite.com/es to www.mysite.com/public/index.php?lang=es
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule /es$ public/index.php?lang=es [L]
#redirect from www.mysite.com/en to www.mysite.com/public/index.php?lang=en
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule /en$ public/index.php?lang=en [L]
#redirect from www.mysite.com/ to www.mysite.com/public/index.php
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(/)?$ public/index.php [L]
#redirect from www.mysite.com/page-NAME/es to www.mysite.com/public/index.php?page=NAME/es
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule page-(.*)/es$ public/index.php?page=$1&lang=es [L]
#redirect from www.mysite.com/page-NAME to www.mysite.com/public/index.php?page=NAME
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule page-(.*)$ public/index.php?page=$1 [L]
#redirect from www.mysite.com/ to www.mysite.com/public/
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/$1 [L]
ok found the problem seems, it was indeed an order issue, this is way seems to work, hopefully this helps someone with the same issue
Options +FollowSymlinks
RewriteEngine on
DirectoryIndex index.php
#Adds www to the domain
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.mysite.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
#redirect from www.mysite.com/page-NAME/es to www.mysite.com/public/index.php?page=NAME/es
RewriteRule page-(.*)/es$ public/index.php?page=$1&lang=es [L]
#redirect from www.mysite.com/page-NAME to www.mysite.com/public/index.php?page=NAME
RewriteRule page-(.*)$ public/index.php?page=$1 [L]
#redirect from www.mysite.com/es to www.mysite.com/public/index.php?lang=es
RewriteRule /es public/index.php?lang=es [L]
#redirect from www.mysite.com/en to www.mysite.com/public/index.php?lang=en
RewriteRule /en public/index.php?lang=en [L]
#redirect from www.mysite.com/ to www.mysite.com/public/index.php
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(/)?$ public/index.php [L]
#redirect from www.mysite.com/ to www.mysite.com/public/
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/$1 [L]

Categories