Rewrite Condition with if-not Condition - mod-rewrite

I've tried now many variations but got no usable result.
First issue: If someone requests http://sub.domain.de this have to be rewrite to https://[...].
Second issue: If in the requested uri is /seafdav than there should be no rewrite.
Here my different variations:
Redirect permanent / https://sub.domain.de/
Redirect permanent /seafdav http://sub.domain.de/seafdav/
RewriteRule ^$ /? [L,R=301] [OR]
RewriteRule ^seafdav/$ /seafdav/? [L,R=301]
RewriteCond %{SERVER_NAME} =sub.domain.de [OR]
RewriteCond %{REQUEST_URI} !^/seafdav.$
RewriteRule ^/?(.*) https://sub.domain.de/$1 [R,L]
RewriteCond %{SERVER_NAME} =sub.domain.de [OR]
RewriteCond %{REQUEST_URI} !^/seafdav.$ [NC]
RewriteRule ^/?(.*) https://sub.domain.de/$1 [R,L]
Thanks in advance for your help!
Found now THE solution! :)
<If "%{REQUEST_URI} !~ /seafdav/">
Redirect permanent / https://sub.domain.de/
</If>

Related

Having difficulty OHS rewrite rule for multiple domains

I'm having a bit of difficulty with rewriting on Oracle HTTP Server for multiple domains that point to same IP address and port
Following is working
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub-doamin-1/psp/UACMP/SELF_SERVICE/SA/c/NUI_FRAMEWORK.PT_LANDINGPAGE.GBL [R,L]
However when I try https://sub-doamin-2/analytic it redirects to the https://sub-doamin-1/psp/UACMP/SELF_SERVICE/SA/c/NUI_FRAMEWORK.PT_LANDINGPAGE.GBL
Tried RewriteCond ${HTTP_HOST} method with no luck. It just redirect to / (root)
RewriteEngine On
RewriteCond ${HTTP_HOST} sub-doamin-1$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub-doamin-1/psp/UACMP/SELF_SERVICE/SA/c/NUI_FRAMEWORK.PT_LANDINGPAGE.GBL [R,L]
RewriteCond ${HTTP_HOST} sub-doamin-2$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub-doamin-2/analytics
Can you please assists resolving this issue?
It should be %{HTTP_HOST} instead of ${HTTP_HOST}
So the rules should be:
RewriteCond %{HTTP_HOST} sub1.test.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub1.test.com/psp/UACMP/SELF_SERVICE/SA/c/NUI_FRAMEWORK.PT_LANDINGPAGE.GBL [R,L]
RewriteCond %{HTTP_HOST} sub2.test.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub2.test.com/analytics [L]
You can check the rules here: https://htaccess.madewithlove.be?share=6632e45c-a7bb-5099-ab0b-468ba1066277
for the urls https://sub1.test.com and https://sub2.test.com
If you write your original rules in that website you will get This test string is not supported: ${HTTP_HOST} so this can also help you next time.

Remove "?" from the query string

I have decided to ditch Wordpress and move to Pico CMS instead.
Now I'm struggling with getting the URL rewriting done correctly.
I have managed to rewrite the query string from eg. https://www.example.org?page to https://www.example.org/page using RewriteRule ^(.*) ?$1 [L], but now I also want it rewritten if there is a query string.
Example:
If https://www.example.org?page is requested it loads just fine, but I want to have it rewritten as https://www.example.org/page - at least in the location bar.
I have tried several variations but to no avail. I suck at regular expressions...
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^\\?/(.*)$ $1
Nope
RewriteRule (^\?$) https://tanghus.net/$1 [NC,R=301,L]
RewriteRule ^.*$ https://tanghus.net/? [NC,R=301,L]
no dice
RewriteCond %{QUERY_STRING} .
RewriteRule ^$ %{QUERY_STRING} [R,L]
Pfff
RewriteCond %{THE_REQUEST} \?\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI} [L,R]
Giving up :(
You can use this Rule
RewriteEngine on
# externally redirect from /?page to /page
RewriteCond %{THE_REQUEST} /\?([^\s]+) [NC]
RewriteRule ^/?$ /%1? [L,R]
# internally map /page to /?page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.+)$ /?$1 [L]

Redirecting non-www to www in IsapiRewrite

I am trying to redirect calls to http://mydomain.com to http://www.mydomain.com.
Is there a simple way to do it in IsapiRewrite file?
I have tried the following but it is not working:
RedirectRule ^mydomain.com$ www.mydomain.com [R=301]
Also tried this:
RewriteCond %{HTTP_HOST} ^mydomain.com [I]
RewriteRule (.*) http\://www.mydomain.com$1 [I,RP]
Any idea what I am doing wrong.
Thanks!
Here's the code:
RewriteEngine on
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]

mod_rewrite with external redirect and internal rewrite

I'm trying to use mod_rewrite to redirect certain pages to use SSL. For that I have:
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_URI} !^/login(\.php)?$ [NC]
RewriteCond %{REQUEST_URI} !^/contact-us(\.php)?$ [NC]
RewriteCond %{REQUEST_URI} !^/\..*$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^dev\.example\.com$ [NC]
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{REQUEST_URI} ^/login(\.php)?$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/contact-us(\.php)?$ [NC]
RewriteRule ^(.+)\.php$ https://www.example.com/$1 [R=301,L]
This works fine, and does exactly what I want it to do.
Later in my .htacess I have a:
RewriteRule ^members/(.+)/change-password$ members/.change-password.php?item=$1 [NC,QSA,L]
So if a URL appears as, for example:
http://www.example.com/members/foo-bar/change-password
Internally it would be processed as:
/members/.change-password.php?item=foo-bar
Again, this works fine and is doing what I want it too.
What I now need to do is include this in my original SSL redirect logic to ensure that any change password requests are redirected to the same URL but over https instead. I've tried:
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_URI} !^/login(\.php)?$ [NC]
RewriteCond %{REQUEST_URI} !^/contact-us(\.php)?$ [NC]
RewriteCond %{REQUEST_URI} !^/\..*$
RewriteCond %{REQUEST_URI} !^/members/.+/change-password [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^dev\.example\.com$ [NC]
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{REQUEST_URI} ^/login(\.php)?$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/contact-us(\.php)?$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/members/.+/change-password [NC]
RewriteRule ^(.+)\.php$ https://www.example.com/$1 [R=301,L]
But this doesn't work - I just get the page delivered over http. Changing the .+ to .* appears to put me into a permanent redirect loop.
I'm guessing this is because of the internal rewrite but no matter what I try I can't seem to resolve it.
Can anyone please advise?
Thanks,
Adam M.
A further review of the mod_rewrite documentation led me to a bit I'd missed specific to its usage in .htaccess files. Basically the [L] flag doesn't actually indicate last as per the norm. Instead you need to use the [END] flag (http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l refers).
Of course that then led me to another issue - my hosting provider doesn't have an up-to-date installation of either Apache or mod_rewrite so the [END] flag triggered the ubiqitous HTTP 500 Internal Server Error.
So what to do? Well I went back to my original ruleset with the knowledge that [L] wasn't doing what I was expecting and spotted the error straight away - the %{REQUEST_URI} value had been updated by the internal rewrite:
RewriteRule ^members/(.+)/change-password$ members/.change-password.php?url-slug=$1 [NC,QSA,L]
Therefore changing my original redirection logic to exclude this resolved my issue:
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_URI} !^/login(\.php)?$ [NC]
RewriteCond %{REQUEST_URI} !^/contact-us(\.php)?$ [NC]
RewriteCond %{REQUEST_URI} !^/\..*$
RewriteCond %{REQUEST_URI} !^/members/.+/change-password$ [NC]
RewriteCond %{REQUEST_URI} !^/members/\.change-password(\.php)? [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^dev\.example\.com$ [NC]
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{REQUEST_URI} ^/login(\.php)?$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/contact-us(\.php)?$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/members/.+/change-password$ [NC]
RewriteRule ^(.+)(\.php)?$ https://www.example.com/$1 [R=301,L]

mod_rewrite - friendly URLs and removing page extension

I need a little help with some mod_rewrite rules I am trying to use. I need to do 2 things
Change a dynamic URL so that it’s friendly (e.g. /content.php?CategoryID=1 change to /categories/1/)
Remove the .php extension on my pages (e.g. /page2.php to /page2)
Ok so I can get number 1 to work by itself with the following in my .htaccess file:
Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteOptions Inherit
RewriteBase /
RewriteRule ^category/([0-9]+)?$ category/$1/ [R]
RewriteRule ^category/([0-9]+)/?$ content.php?CategoryID=$1
I am then trying number 2 and I'm running into a few problems. I am using the following:
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\ [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(/.+)\.php$
RewriteRule ^(.+)\.php$ %1/ [R=301,L]
RewriteCond %{REQUEST_URI} ^(/.+)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^.+/$ %1.php [QSA,L]
The above rerwrites the URL so it looks friendly (e.g. /page2/) but it hits my 404 page and I can't view it but my other page where I rewrite the URL still works (e.g. /category/1/).
This is my full .htaccess file:
Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteOptions Inherit
RewriteBase /
RewriteRule ^category/([0-9]+)?$ category/$1/ [R]
RewriteRule ^category/([0-9]+)/?$ content.php?CategoryID=$1
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\ [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(/.+)\.php$
RewriteRule ^(.+)\.php$ %1/ [R=301,L]
RewriteCond %{REQUEST_URI} ^(/.+)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^.+/$ %1.php [QSA,L]
ErrorDocument 404 /misc/404page.html
Any help would be greatly appreciated as I am new to all this mod_rewrite and regex stuff.
I know it's been a while, and I'm not 100% sure what you were trying to do, but I would probably be looking at:
RewriteRule ^category/([0-9]+)$ category/$1/ [R,L]
RewriteRule ^category/([0-9]+)/$ content.php?CategoryID=$1 [L]
RewriteRule \.php? - [L]
RewriteRule ^(.+)\.php$ $1 [R,L]
RewriteRule ^([^/?])$ $1.php [L]
I'd need to look at your .htaccess more closely and/or have a better idea of the structure of your site to give better information.

Resources