Rewrite in apache server - apache

I've got the following in my httpaccess file on my apache http server.
RewriteEngine on
RewriteCond %{THE_REQUEST} /web/([^\s]+) [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteRule ^((?!web).*)$ /web/$1 [NC,L]
and it's working properly. But i wan to apply this condition for all urls except when I ask for /correoimages/ then I want that the url be the same url that is requested. How can I do that?
That is when I ask for https://example.org/CORREOIMAGES/a.jpg the url must be https:///example.org/correoimages/a.jpg and not https://example.org/web/correoimages/a.jpg. In the rest of the case web must be added.
I dont´understand properly RewriteRule and RewriteCond.

You can simply add an exception to your rule set:
RewriteEngine on
RewriteCond %{THE_REQUEST} /web/([^\s]+) [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteRule ^correoimages/ - [L]
RewriteRule ^((?!web).*)$ /web/$1 [NC,L]

Related

htaccess rewrite if url doesnt have a query string

I have a htaccess that rewrites to /login if conditions aren't met.
It's working fine for urls without query_string.
However I have no clue how to include /reset?query_string to accepted conditions. And want to exclude /reset
/reset?6tdsBMxpeeJEDUvYzwKmLzIusLYLjgMtIj
RewriteEngine On
RewriteBase /index.php
RewriteCond %{REQUEST_URI} !^/login$
RewriteCond %{REQUEST_URI} !^/forgotten$
RewriteRule ^([^\.]+)$ /login [R=301,L,QSD]
RewriteRule ^(/)?$ /login [R=301,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Here is a modified version of your .htaccess that excludes /reset?query:
RewriteEngine On
# /reset with query string
RewriteCond %{QUERY_STRING} .
RewriteRule ^(reset)/?$ $1.php [L,NC]
RewriteCond %{REQUEST_URI} !^/(forgotten|login)$ [NC]
RewriteRule ^([^.]*)$ /login [R=301,L,QSD]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)$ $1.php [L]
Make sure to test it after clearing your browser cache.
With your shown samples and attempts, please try following htaccess rules file. Make sure to clear your browser cache before testing your URLs.
Also I am making use of apache's variable named THE_REQUEST by which we can handle both uri as well as query string.
RewriteEngine ON
RewriteBase /
##Rules for handling rest uri without query string.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^reset/?$ /login? [R=301,L,NC]
##Rules for reset with uri and query string.
RewriteCond %{THE_REQUEST} \s/reset\?\S+\s [NC]
RewriteRule ^ /reset? [R=301,NC]
##Rule for login page's backend rewrite.
RewriteRule ^login/?$ login.php [QSA,NC,L]
##Rule for forgotten page's backend rewrite.
RewriteRule ^forgotten/?$ forgotten.php [QSA,NC,L]
##Rules for non-existing pages should be served with index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

Need to redirect 301 a URL in .htaccess file but it adds extra http//?

I am trying to redirect /abc.html to /abc.php but when I did it gives an extra http// and page is not working like http//www.example.de/abc.php don't know from where this HTTP comes.
note: website is not with ssl so domain name is http://example.de
My .htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://example.de/$1 [R=301,L]
RedirectPermanent /tour.html /tour.php
With your shown samples/attempts, could you please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
##To serve home page link.
RewriteRule ^/?$ index.php [L]
RewriteCond %{REQUEST_URI} !^/?$
RewriteRule ^([^.]*)\.html/?$ $1.php [NC,L]

.htaccess Redirect all pages except one and another internal redirect of a certain domain

I want to redirect all request of one particular domain to another, except for one page request. I hope my attempt explains what I try to do:
RewriteEngine on
Rewritecond %{HTTP_HOST} !^host\.mysite\.com
RewriteRule ^link/([^/]+)/([^/]+)/([^/]+)$ dokument_by_link.php?$1=1&document_type=$2&value=$3 [NC,L]
Rewritecond %{HTTP_HOST} !^host\.mysite\.com
RewriteCond %{REQUEST_URI} !^/link/
RewriteCond %{REQUEST_URI} !^dokument_by_link\.php
RewriteRule (.*) https://www.anothersite.de/$1 [R=302,L]
#This should not apply to host.mysite.com, but I think this is already accomplished by the L-flag above
RewriteRule ^test/?$ test.php [L,NC]
host.mysite.com/link/ should be redirected to host.mysite.com/document_by_link.php
RewriteRule ^link/([^/]+)/([^/]+)/([^/]+)$ dokument_by_link.php?$1=1&document_type=$2&value=$3 [NC,L]
All other requests should be redirected to https://www.anothersite.de
Thank you very much!
This should do the trick :
RewriteEngine on
#rewrite /link to /document_by_link.php
RewriteRule ^link/?$ /document_by_link.php [L]
#redirect all other URLs to https://www.anothersite.de
RewriteRule !dokument_by_link\.php$ https://www.anothersite.de%{REQUEST_URI} [L,R]
This should work for you:
RewriteEngine On
Rewritecond %{HTTP_HOST} ^host\.mysite\.com$ [NC]
RewriteCond %{THE_REQUEST} !\s/link/ [NC]
RewriteRule ^ https://www.anothersite.de%{REQUEST_URI} [R=302,L,NE]
Rewritecond %{HTTP_HOST} ^host\.mysite\.com$ [NC]
RewriteRule ^link/([^/]+)/([^/]+)/([^/]+)$ dokument_by_link.php?$1=1&document_type=$2&value=$3 [NC,L,QSA]
RewriteRule ^test/?$ test.php [L,NC]
Using THE_REQUEST instead of REQUEST_URI here as REQUEST_URI may change to a rewritten URI whereas THE_REQUEST remains same for the scope of a web request.
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1

Hiding two GET METHOD in url using htaccess

I have a website example.com and I am passing two GET parameters in the url.
example.com/page.php?page=5&section=10
Now I want it to show
example.com/5/10
I've already got it to work for the first part but cannot seem to figure out the second part (section part)
My current .htaccess file is
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+page\.php\?page=(\d+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteRule ^(\d+)/?$ page.php?page=$1 [L,QSA,NC]
All I need to get is the second part working (&section=10)
You can use this
RewriteEngine on
#redirect and rewrite the single get perm /?page
RewriteCond %{THE_REQUEST} \s/+page\.php\?page=(\d+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteRule ^(\d+)/?$ page.php?page=$1 [L,QSA,NC]
#redirect and rewrite urls with multiple perms
RewriteCond %{THE_REQUEST} \s/+page\.php\?page=(\d+)&section=(\d+) [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]
RewriteRule ^(\d+)/(\d+)/?$ page.php?page=$1&section=$2 [L,QSA,NC]
Add this to your .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} page=(.+)&section=(.+)$ [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]
This grabs the variables using {QUERY_STRING} and rewrites your URL to http://example.com/5/10. The use of the ? on the end of the rewrite is to stop the query from appending onto the end after the rewrite.
Make sure you clear your cache before testing this.

Get utl parameters / URL rewriting

I'd like to use URL rewriting to have "virtual" subdomains. I already created wildcard DNS entry.
I want subdomain to be added as GET parameter.
Example:
http://mysubdomain.domain.com/index.php
would call:
http://www.domain.com/index.php?subdomain=mysubdomain
and
http://mysubdomain.domain.com/page.php?parameter1=parameter
would call:
http://www.domain.com/page.php?parameter1=parameter&subdomain=mysubdomain
This is my .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+).domain.com$ [NC]
RewriteRule ^$ /index.php?subdomain=%2 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+).domain.com$ [NC]
RewriteRule ^([A-Za-z0-9_.]+)$ /$1 [L]
It works but I cannot get subdomain value, when calling other page than index.php, if I call http://subdomain.domain.com/page.php I loose subdomain parameter, I tried:
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+).domain.com$ [NC]
RewriteRule ^([A-Za-z0-9_.]+)$ /$1&subdomain=%2 [L]
But I got Internal error.
THis rule:
RewriteRule ^$ /index.php?subdomain=%2 [L]
needs a QSA flag:
RewriteRule ^$ /index.php?subdomain=%2 [L,QSA]
and this rule:
RewriteRule ^([A-Za-z0-9_.]+)$ /$1 [L]
needs the QSA flag and the proper backreference:
RewriteRule ^([A-Za-z0-9_.]+)$ /$1?subdomain=%2 [L,QSA]