I'm trying to write a rule that when user types in this url:
domain.com/09/13/2013/thisIsMyPageTitle
That url stays in browser window, but content from this url is displayed:
domain.com/contentlibrary/thisIsMyPageTitle
This is my rule that I currently get an error with:
RewriteEngine On
RewriteRule ^((0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d[/])$(.*) /contentlibrary/$1 [L]
I'm trying to match the date with regular expression, and use the (.*) from the initial url in the second one that holds the content and actually exists.
If you're not going to do anything with date then why bother being precise with date semantics. You can simplify your regex:
RewriteRule ^[0-9]+/[0-9]+/[0-9]+/([^/]+)/?$ /contentlibrary/$1 [L]
The error that you're getting is probably because you have unescaped spaces in your regex. Specifically these:
[- /.]
The spaces get interpreted by mod_rewrite as the delimiter between parameters. Additionally, you have this:
$(.*)
at the end of your pattern. The $ matches the end of the string, so you want those swapped:
(.*)$
So:
^((0[1-9]|1[012])[-\ /.](0[1-9]|[12][0-9]|3[01])[-\ /.](19|20)\d\d[/])(.*)$
shold be the pattern that you want.
Related
I am redirecting certain urls with path to get variables like the following:
localhost2/post/myTitle => localhost2/post.php?title=myTitle
localhost2/post/123 => localhost2/post.php?id=123
So In my htaccess file, I use
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^post/(\d+) post.php?id=$1
RewriteRule ^post/(.*) post.php?title=$1
</IfModule>
This works no problem. But I want to learn how to write negative of ^post/(\d+), that is ^post/(NEGATE-ONLY-NUMBERS). In other words I want a regex that matches the whole input sting if there is not only numbers after post/. So post/abc, post/a23, post/ab3, post/12c and post/a2c should all pass but not post/123. I refered to this post, which suggest using:
(?!^\d+$)^.+$
I can't use ^post/(?!^\d+$)^.+$, because there can be only one ^ and one $. I don't know what regex anchor specifies first position in a substring. My best guess is
post\/(?!\d++).*
I think (?!\d++), with the ++ would eat all characters followig and check if all are digits. But this fails at post/1ab.
Another guess is:
post\/(?![\d,\/]+$).*
The works the best but it allows: post/3455/X.
Secondly, eventually I need to convert localhost2/post/myTitle/123 => localhost2/post.php?title=myTitle&repeat=123 as well. I ave come up with the following:
^post/(?!\d+($|/))(.+?($|/))(\d+$)?
Note: +? to use lazy quantifier, otherwise multiple slashes will be matched by .
and
^post/(?!\d+($|/))([^/\n\r]+($|/))(\d+$)?
Here I use [^/\n\r] instead of .+?
Patterns inside zero-width assertions like (?!\d++) are non-consuming, they do not "eat" chars, they only check the context while keeping the regex index at the same location as before matching the zero-width assertion pattern.
You can use any of the following:
^post/(?!\d+(?:/|$)).*
^post/(?!\d+(?=/|$)).*
^post/(?!\d+(?![^/])).*
See the regex demo. Details:
^post/ - start of input, post/ literal string
(?!\d+(?=/|$)) - a negative lookahead that fails the match if, immediately to the right of the current location, there are one or more digits followed with / or end of string
.* - the rest of the input.
Do not over complicate things when you can keep things simple by keeping 3 separate rewrite rules and since your query parameters are named differently you will need 3 separate rewrite rules anyway.
Consider:
Options -MultiViews
RewriteEngine On
RewriteRule ^post/(\d+) post.php?id=$1 [L,QSA,NC]
RewriteRule ^post/([^/]+)/(\d+) post.php?title=$1&repeat=$2 [L,QSA,NC]
RewriteRule ^post/([^/]*) post.php?title=$1 [L,QSA,NC]
Take note of Options -MultiViews. If this is not enabled in Apache config you must have it here otherwise it will keep all $_GET parameters empty in your php file.
Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.html.
I have some links as follow.
http://www.example.com/excel.aspx
http://www.example.com:80/word.aspx
http://www.example.com:80/powerpoint.aspx
http://www.example.com:80/pdf.aspx
http://www.example.com:80/barcode.aspx
http://www.example.com/ocr.aspx
http://www.example.com/email.aspx
http://www.example.com/project.aspx
& I want to rewrite the URLs as following in a single regex pattern or single regex line.
https://www.example.com/excel
https://www.example.com/word
https://www.example.com/powerpoint
https://www.example.com/pdf
https://www.example.com/barcode
https://www.example.com/ocr
https://www.example.com/outlook
https://www.example.com/project
If you check & compare, I have replaced email with outlook in substitution URL.
I can match patterns as follow .*/(\w+).aspx and substitute as https://www.example.com/$1 but I cannot find a way to replace email with outlook. Demo https://regex101.com/r/71huIx/24
Something similar to http://www.rexegg.com/regex-trick-conditional-replacement.html but without a pool or dictionary. However, if the pool or dictionary can be part of regex itself then it will OK. Reason being, I have txt file with rules, where line1 is regex & line2 is a substitution.
Any ideas how can I do that?
Here's an Apache httpd config that should work:
RewriteEngine On
RewriteRule ^/outlook$ https://example.com/email.aspx [P,L]
RewriteRule ^/([^./])/?$ https://example.com/$1.aspx [P,L]
This uses a reverse proxy to serve pages like email.aspx and foobar.aspx when given "outlook" and "foobar" without revealing their extensions to users.
Though it looks like you're trying to go backwards for some reason. In that case, you'd want:
RewriteEngine On
RewriteRule ^/email\.aspx$ https://example.com/outlook [L]
RewriteRule ^/([^.]+)\.aspx$ https://example.com/$1 [L]
Regex conditional expressions cannot perform your task. The structure of a regex conditional is:
(?(condition)yes-pattern|no-pattern)
condition is either
a zero-width assertion (typically a lookahead)
a parenthesized integer, denoting whether that numbered capture has content
a capture group name in angle brackets or single quotes
(R) which is true when evaluated inside a recursion or eval
yes-pattern is a regex to match given the condition is true
no-pattern is an optional regex to match given the condition is false
Therefore, you can't shove new text into the match using a conditional.
I'm new to regular expressions and need to rewrite an example URL:
http://domain.com/quiz.php?id=1
To the friendly URL,which looks like forum URL, like this:
http://domain.com/1-quiz-title
So 1 is the GET variable. Title must be lowercase only
I tried the following but it seems incorrect:
RewriteRule ^([a-z0-9\-]+)$ quiz.php?id=$1 [L]
It's picking the GET variable as 1-quiz-title while it should be only 1
Thanks
If the get variable is only numbers, you want the regex to be like this:
RewriteRule ^([0-9]+)- quiz.php?id=$1 [L]
So the regex matches some amount of numbers first and groups it (the parentheses), the matches a "-". Note that there isn't a $ for end of match, this is essentially going to ignore the title completely, not even going to try to match it. The title doesn't need to be in the rewritten URL so we don't really care what comes after the -.
I would like to use mod_rewrite to capture a string within brackets in my URL and do a redirect.
My URL:
something?var_a=A&var_b=(B)&var_c=C
my .httaccess file with the regex:
RewriteEngine on
RewriteRule ^/?.+var_b=\((.*)\)$ somedir/$1 [R]
I just would like to capture what's in between the round brackets, so my redirect should look something like this: somedir/B
I test my regex at http://htaccess.madewithlove.be/ but I get no match.
I don't know what I am missing here, even if I try much simpler regexes, e.g. .+var_b(.*)$ I get no match. Only if my regex was looking for a pattern at the beginning, I get a match, so for example the regex something(.*)$ works.
What am I missing here?
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)var_b=\((.*?)\)(&|$) [NC]
RewriteRule ^.*$ somedir/%2? [R]
The reason is that RewriteRule does not receive the ?x=y part of the query. The %2 variable refers to the pattern from the last RewriteCond, while $2 would refer to the pattern from this RewriteRule. The ? at the end prevents the query part ?x=y from being automatically appended at the end of the result.
The (^|&) and (&|$) in the pattern guarantee that var_b=(B) is the complete parameter and not a part of it. Without these, the pattern would also match ?xyzvar_b=(B) or ?var_b=(B)xyz. With these, it will only match ?var_b=(B) or ?a=b&var_b=(B)&x=z etc.
I have done this: http://rubular.com/r/AHI15Tb4ju, and it match the second url (http://gamempire.localhost.it/news/tomb-pc), but I want to exclude that url and match everything that do not have the word "news/" inside (but at the same time end in the way that I have specified).
How to do that?
Basically, i want to match only the third url (http://gamempire.localhost.it/tomb-pc).
Thanks!
You can use a rule like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/news/
RewriteRule -(?:pc|ps2|ps3|ps4|xbox-360|xbox-one|xbox|wii-u|wii|psp|ps-vita|ds|3ds|iphone|ipad|android|playstation)(.*)$ / [L,R]
Since I didn't know any action part I just redirected these matching URI patterns to / that you can change according to your need.
Try using this:
^((?!news).)*-(?:pc|ps2|ps3|ps4|xbox-360|xbox-one|xbox|wii-u|wii|psp|ps-vita|ds|3ds|iphone|ipad|android|playstation)(.*)$
It should be noted that I tried to modify your original pattern as little as possible, assuming you also needed the (.*) at the end even though it appears that this is unnecessary for your purposes, and would match strings such as
"http://gamempire.localhost-pc.it/tomb" and "http://-pcgamempire.localhost.it/tomb".