Here is the config I am using
RewriteEngine on
RewriteMap shortlinks txt:/var/www/html/s.overhash.net/public_html/shortlinks.txt
RewriteRule ^/(.+)$ ${shortlinks:$1} [R=temp,L]
My txt document looks something like this:
9H40o https://osyrisrblx.github.io/playground/#code/HYUw7gBAggTjCGBPAPMArgWwEYhgPgAoBKAOhhABM0BjEAggB3IDcAaCatOEYAFyIC8eJiGYBqAZ258iQA
However, upon going to my site (http://s.overhash.net/9H40o), it replaces the # with %23, making the URL this:
https://osyrisrblx.github.io/playground/%23code/HYUw7gBAggTjCGBPAPMArgWwEYhgPgAoBKAOhhABM0BjEAggB3IDcAaCatOEYAFyIC8eJiGYBqAZ258iQA
(which isn't valid)
How would I go about ensuring the # remains?
It's not a rewrite map issue, it's the way rewrite rules work.
By default, special chars will be escaped.
Use the NE flag in your rewriterule if you don't want that to happen:
RewriteRule ^/(.+)$ ${shortlinks:$1} [NE,R=temp,L]
More details on https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_ne
Related
Hello !
I know there already are a lot of topics about URL rewritting and I honestly swear I've spent a lot of time trying to apply them to my problem but I can't see any of them perfectly applying to my situation (if you find otherwise, please give the link).
-----
Here's the problem :
I'm learning MVC model and URL rewriting and I have my URL like this :
http://localhost/blahblahblah/mywebsite/index.php?param1=value1¶m2=value2¶m3=value3 ... etc ...
What I want (for some MVC template goals) is to have this kind of URL :
http://localhost/blahblahblah/mywebsite/value1/value2/value3 ... etc ...
-----
Whatever are the names of the parameters and whatever are the values.
This is the most essential thing I can't find a solution for.
(Also don't mind the localhost blahblahblah, this has to work even on distant websites but I trust it will work fine on online website has this part of URL may have no importance in what I want to do)
Thanks a lot for your time if you can help me seeing clearer in what I need to do.
If the .htaccess file is located in the document root (ie. effectively at http://localhost/.htaccess) then you would need to do something like the following using mod_rewrite:
RewriteEngine On
RewriteRule ^(blahblahblah/mywebsite)/(\w+)$ $1/index.php?param1=$2 [L]
RewriteRule ^(blahblahblah/mywebsite)/(\w+)/(\w+)$ $1/index.php?param1=$2¶m2=$3 [L]
RewriteRule ^(blahblahblah/mywebsite)/(\w+)/(\w+)/(\w+)$ $1/index.php?param1=$2¶m2=$3¶m3=$4 [L]
# etc.
Where $n is a backreference to the corresponding captured group in the preceding RewriteRule pattern (1st argument).
UDPATE: \w is a shorthand character class that matches a-z, A-Z, 0-9 and _ (underscore).
A new directive is required for every number of parameters. You could combine them into a single (complex) directive but you would have lots of empty parameters when only a few parameters were passed (rather than not passing those parameters at all).
I'm assuming your URLs do not end in a trailing slash.
If, however, the .htaccess file is located in the /blahblahblah/mywebsite directory then then directives could be simplified a bit:
RewriteRule ^(\w+)$ index.php?param1=$1 [L]
RewriteRule ^(\w+)/(\w+)$ index.php?param1=$1¶m2=$2 [L]
RewriteRule ^(\w+)/([\w]+)/([\w]+)$ index.php?param1=$1¶m2=$2¶m3=$3 [L]
# etc.
Don't use URL parameters (alternative method)
An alternative approach is to not convert the path segments into URL parameters in .htaccess and instead just pass everything to index.php and let your PHP script split the URL into parameters. This allows for any number of parameters.
For example, your .htaccess file then becomes rather more simple:
RewriteRule ^\w+(/\w+)*$ index.php [L]
(This assumes the .htaccess file is located in /blahblahblah/mywebsite directory, otherwise you need to add the necessary directory prefix as above.)
The RewriteRule pattern simply validates the request URL is of the form /value1 or /value1/value2 or /value1/value2/value3 etc. And the request is rewritten to index.php (the front-controller) to handle everything.
In index.php you then examine $_SERVER['REQUEST_URI'] and parse the requested URL.
Is it possible to write a 301 Apache redirect and preserve a portion of the URL?
For example, I have pages that include -md in the title and want to redirect them to the same URL just without the trailing -md. I want to preserve the test-page-1 and test-page-2 portion, but I'm unsure how to write the Rewrite rule to achieve this.
/doctors/test-page-1-md ---> /pages/test-page-1
/doctors/test-page-2-md ---> /pages-test-page-2
How would I write the rewrite rule to look for the /test-page-1-md after the /doctors?
How would I preserve the test-page-1 and test-page-2 portions?
I've been working with this to start but I don't appear to be having much luck. I think my problem lies with the $1 variables.
RewriteRule ^doctors/$1-md /doctors/$1
This is RewriteRule syntax:
RewriteRule Pattern Substitution [flags]
Pattern is a perl compatible regular expression. The Substitution of a rewrite rule is the string that replaces the original URL-path that was matched by Pattern. So, in order to use $1 variable in Substitution, you first need to match it in Pattern :
RewriteEngine on
RewriteRule ^doctors/(.+)-md /pages/$1 [R=302]
(the above should work in .htaccess; if you use it in VirtualHost configuration, you have to start the Pattern with leading /, so: ^/doctors/(.+)-md). You can also change 302 to 301 once you are sure that everything is working as expected.
I'm want to rewrite many pages following the simple schema:
old_page.php to content/new-page.php
I got a problem when apple.php must be rewritten to fruits/red-apple.php because I get into a rewriting loop, so i have to match domain/apple.php
I'm not sure if I can set up RewriteRule or I'll also need rewriteCond ..
You should show us what you're working with, but it sounds like your patterns are simply missing start and end of string anchors. E.g.:
RewriteBase /
RewriteRule ^apple\.php$ fruits/red-apple.php [NS,L]
You may be better off using RewriteMap if you have a lot of rules.
I want to translate the following URL:
http://my.domain.net/locations
to
http://my.domain.net/location_list.php
My current rewrite rule looks like this:
RewriteRule ^(locations)$ /$1_list.php
But that mean that my file must be named locations_list.php. So ideally I would like to remove the 's' from my variable $1, is this possible? I'm having problems finding any examples.
Just remove the s:
RewriteRule ^(location)$ /$1_list.php
or make it optional
RewriteRule ^(locations?)$ /$1_list.php
or make it so it's not captured
RewriteRule ^(location)s$ /$1_list.php
I have been developing a shop, which uses mod_rewrite to allow us to make the URIs more readable, for instance:
http://www.example.com/shop/Tools
Will be rewritten to
http://www.example.com/index.php?area=shop&folder=Tools
My rewrite rule is as follows:
RewriteRule ^shop/([^?#]+) index.php?area=shop&folder=$1 [NC,QSA,L]
However, this breaks when the folder name ends in . (dot), as I discovered when testing with a folder name ending in "etc."
It seems any trailing dots are totally removed before $_GET has been populated. If I put another character after the dot, it's fine, if the URI ends in any number of dots, they are removed
Is there a way to stop this from happening?
You don't need to exclude "?" and "#" in RewriteRule since it operates only on URI (path), without query-string or anchor.
So, this is enough:
RewriteRule ^shop/(.+) index.php?area=shop&folder=$1 [NC,QSA,L]
That being said, this does not change the fact that dots get stripped.
This may be a result of MultiViews being on. This option makes Apache try and resolve the URIs disregarding extensions.
So, add this as well:
Options -Multiviews