htaccess forward but does not uses parameter - regex

I have a quite simple rewrite rule, it works meaning the rewrite works but the parameter does not reach the destination page.
Each page has a section, title and a id.
The url has dashes in the name and I use a underscore separator to separate the id.
The section name is hypnose
The title is Hoe-werkt-hypnose
The id is 4
The rewrite rule:
RewriteRule ^hypnose/([^/.]+)_([^/.]+).php$ hypnose.php?title=$1&id=$2 [L]
The url that i'm using
hypnose/Hoe-werkt-hypnose_4.php

You need to turn MultiViews option off:
Options -MultiViews
RewriteEngine On
RewriteRule ^hypnose/([^/_]+)_([^/.]+)\.php$ hypnose.php?title=$1&id=$2 [L,QSA,NC]
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.

Related

htaccess redirect starting URL expression to blog page

I want to redirect certain URLs starting with an expression. For ex
I want to redirect:
www.example.com/%2FE (www.example.com/%2FExxxxxxxx) to my blog page in my .htaccess file.
I can redirect www.example.com/2FExxxxx but I am not able to target the %.
The xxxx... I have used in the URL is to represent any expression after %2FE.
This is my code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule %2FE /blog [R=301,L]
<IfModule>
Can anyone here help me?
By default Apache rejects (with a server generated 404) any URL that contains an encoded slash (%2F) in the URL-path part of the URL. This occurs before the request is processed by .htaccess. (This is considered a security feature.)
To specifically permit encoded slashes, there is the AllowEncodedSlashes directive (default value is Off). But this can only be set in a server or virtualhost context. It cannot be set in .htaccess. To permit encoded slashes, AllowEncodedSlashes can be set to either On or NoDecode (preferable).
For example:
# In a server / virtualhost context (not .htaccess)
AllowEncodedSlashes NoDecode
Then, once the above has been implemented in the server config and the webserver restarted, you can proceed to match the slash using mod_rewrite in .htaccess...
RewriteRule %2FE /blog [R=301,L]
Ordinarily, the RewriteRule pattern matches against the %-decoded URL-path. However, if the NoDecode option has been set then the encoded slash (%2F) is not decoded. So the above "should" work (except that the pattern is not anchored, so potentially matches too much).
But note that multiple (decoded) slashes are reduced in the URL-path that is matched by the RewriteRule pattern. So matching multiple-contiguous slashes here is not possible.
I would instead match against the THE_REQUEST server variable, which is as per the original request and always remains %-encoded (if that is how the request has been made). And multiple slashes are preserved. Note that THE_REQUEST contains the first line of the HTTP request headers, not just the URL-path.
For example:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/%2FE [NC]
RewriteRule . /blog [R=301,L]
You should not use the <IfModule> wrapper here.

How to write this URL rewrite rule to trucate unwanted content?

Iam using apache mod_rewrite to redirect some URL. I been able to some of the URLS but none of my rules are able to caprure this
http://192.23.70.11/mome/GIS%20Endo/%2Fhome%2FGIS%2520Endo%2F
and redirect it to
http://192.23.70.11/mome/GIS%20Endo/
You can use this rule as your first rule in site root .htaccess or Apache config:
# this goes in httpd.conf
AllowEncodedSlashes On
RewriteEngine On
RewriteRule ^(/?mome/[^/]+)/.+$ /$1 [L,NC,R=301,NE]
This will match any URI starting with /mome and will truncate anything after 2nd path component.

Apache LocationMatch regex ignores duplicate slashes

I'm having a strange behaviour with Apache's LocationMatch directive when there are extra slashes at the beginning of the URL. According to the Apache docs if I'm reading it right this should work:
<LocationMatch ^/appcontext/(a|b)>
SetHandler weblogic-handler
WebLogicCluster apphost01:xxxx,apphost02:xxxx
WLProxySSL ON
</LocationMatch>
However if I type the following URL it is also being forwarded to the backend hosts:
https:// <hostname:port> ////////appcontext/a/
In the Apache docs it clearly states that it should apply the directive only for /appcontext/a/, unless I'm missing something with the regex or there is some issue with the mod_wl plug-in
http://httpd.apache.org/docs/2.2/mod/core.html
"For example, <LocationMatch ^/abc> would match the request URL /abc but not the request URL //abc"
Does anyone have any suggestion to achieve this?
Yes Apache does collapse extra slashes in rewrite rule. You can use THE_REQUEST variable to match and remove extra slashes. Place this in root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*?)//+(.*)$
RewriteRule ^ %1/%2 [R=302,L,NE]

Seo friendly url for specific page

I need to make a specific page url to user friendly
I have a page www.example.com/index.php?route=a/b
Which I want to be rewritten as -> www.example.com/a
I used this rule in htacces but its not working
RewriteRule ^/a$ index.php?route=a/b
Please help
Change your rule to:
RewriteEngine On
RewriteRule ^a/?$ /index.php?route=a/b [L,NC,QSA]
And remember that inside .htaccess leading slash in URI is not matched so ^a/?$ instead of ^/a/?$.
Reference: Apache mod_rewrite Introduction

Using mod_rewrite to redirect unsupported language codes

I'm developing a multilingual site, and I'm using language identifiers in urls in conjunction with a front controller such that visiting
http://www.domain.tld/en/content
or
http://www.domain.tld/de/conent
will pull up content localised in those languages. If the site is visited without specifying a language identifier, the users browsers language settings are detected via Accept-Language as a best guess, and then redirected. If Accept-Language cannot be detected, then it defaults to english. So, for example, if you visit
http://www.domain.tld/content
and your browsers language settings are in german and Accept-Language detects de, you'll be redirected to
http://www.domain.tld/de/content
If your browsers language settings are not supported, you'll be redirected to the default supported language, english
http://www.domain.tld/en/content
This works fine... as long as don't manually put in a url with an unsupported language identifier at the beginning of the url. To explain, I have mod_rewrite set up such that urls that dont point to existing files or folders invoke the front controller. That being the case, if I purposefully put in an unsupported language code in the browser, such as 'it',
http://www.domain.tld/it/content
The front controller is invoked, but I want to avoid this, instead prefering to have Apache filter requests such that only supported languages are provided to the front controller.
Essentially what I want is to be able to define a rule such that if a url does not start with a supported language identifier, that users will abe redirected to a url that replaces the unsupported language code with the default one (en) without invoking the front controller.
This is my config file:
#
# Access rights and behaviours for DocumentRoot
#
<Directory /WWW>
#
Options +FollowSymLinks +Indexes -MultiViews
# Allow requests for all
Order allow,deny
Allow from all
# Don't use access rules defined in child directories
AllowOverride None
# Define the router as the default index page
DirectoryIndex /router.php
# Turn on url rewriting
RewriteEngine on
# If we encounter a request to document root and can detect a language preference, grab it...
RewriteCond %{HTTP:Accept-Language} ^(en|de|fr)[-,;]? [NC,OR]
# ... or use the default language identifier (english) ...
RewriteCond en ^(en)$
# ... and redirect the request to a path prefixed with the language identifier
RewriteRule ^$ /%1/ [R=301,L]
# Mystery rule here!
# If the requested file does not exist...
RewriteCond %{REQUEST_FILENAME} !-f
# ... or if the requested directory does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# ...forward the request to the router
RewriteRule ^(.*)$ router.php/$1 [L]
</Directory>
Where I've put the "Mystery rule here!" comment is where I need to define a rule such that if the request uri does not begin with a supported language code, that en will be used in place.
I've tried doing this a number of ways, but keep falling into redirect loops. To example what I'm trying to do
RewriteCond %{REQUEST_URI} !^/(en|de|fr).$
RewriteRule ^$ /en/$1 [R=301,L]
What this is supposed to represent is:
If the request uri does not begin with en, de or fr, grab the remainder of the uri, prefix it with en, then redirect for the front controller to intercept.
Can anyone advise me? Thanks!
Try this rule:
RewriteRule !^(en|de|fr)/ /en%{REQUEST_URI} [R=301,L]
This will add /en at the begin of the URI path if it does not already start with either /en, /de, or /fr.