I'm using ModRewrite to send requests for
/2/Blog-Title
to
/?post=2&title=Blog-Title
Originally I used ([A-Za-z0-9-_/]+), but then realised that people were using lots of punctuation in their titles. I've gradually added lots more punctuation, but am feeling that this is the wrong way to go about this... I'm now using
RewriteRule ^([0-9]+)\/([A-Za-z0-9-_/\.\?\!':\&]+)$ /?post=$1&title=$2 [L]
How can I ignore any requirements for the title, and just match any text following the number? (I don't actually need the title= bit set, the text is really for SEO, not for internal working.)
If you want to ignore title just do:
RewriteRule ^([0-9]+)\/.*$ /?post=$1 [L]
.* will match any string after / so it will matches anythong like:
/2/lores-ipsum_etc356
Related
I have a couple of RewriteRules that I'd like to combine into one. I'm not sure how to do it, though. I think look arounds would need to be used? The difference between the two is the first would match something like:
search/foo or search/foo/
and the second would match
search/foo/10 or search/foo/10/
Rewrites:
RewriteRule ^search/([a-zA-Z]+)/?$ index.php?page=search&query=$1&pn=1 [L]
RewriteRule ^search/([a-zA-Z]+)/([0-9]+)/?$ index.php?page=search&query=$1&pn=$2 [L]
Without using look arounds, my attempt would be
^search/([a-zA-Z]+)/?([0-9]+)?/?$
But i think that would match something undesirable like this?
search/foo10
edit:
I'm tryin to get the regex to match the following URIs:
search/foo
search/foo/
search/foo/1
search/foo/1/
^search/([a-zA-Z]+)(/)?([0-9]+)?\/?$
will this work? you might have make '/' also as match pattern and ignore $2.Same can be followed for the trailing '/' and ignore $4.
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".
RewriteRule ^categories/([A-Za-z0-9\-]+)/?$ /categories.php?c=$1 [QSA,L]
This is my RewriteRule, it deals with categories such as /categories/Family perfectly that URL displays the page as I would like it too, However with something such as /categories/Web%20Design I get an The requested URL /categories/Web Design was not found on this server.
This is a pain, I've even tried to use a space in the ReWriteRule after the 9 in [A-Za-z0-9-], what's the best way to handle spaces in URLs with the rewrites?
Thank You All.
Use this rule by including space in your character class:
RewriteRule ^categories/([A-Za-z0-9\s-]+)/?$ /categories.php?c=$1 [QSA,L]
try adding \s to the Regex...
That matches whitespace.
([A-Za-z0-9\-\s]+)
So, I have the following RegEx..
RewriteRule ^([-a-z0-9]*[A-Z\.]+.*)$ file.php?string=$1 [QSA]
The URL I want file.php to trigger for must either have capital letters or a period in it, then send the URL to the PHP script.
However, the problem I have is that this script is triggering on any URL, because of the not-truly-escaped Period.
I've tried escaping the period with a backslash, or two backslashes, or three... but none stop the generic interpretation.
What am I doing wrong?
Edit: As an example,
RewriteRule ^([-a-z0-9]*[A-Z\\.]+[-a-z0-9\/]*)$ file.php?string=$1 [QSA]
Doesn't work, but
RewriteRule ^([-a-z0-9]*\\.+[-a-z0-9\/]*)$ file.php?string=$1 [QSA]
does escape it.
Edit 2: Examples of URLs I want to redirect:
/some-page-goes-here.html
/heres-Robs/Old/Page/
And ones I don't:
/testing/one/two/
/an/actual-file.gif
EDIT 3: Old regex was:
RewriteRule ^([-a-z0-9]*[A-Z\.]+[-a-z0-9\/]*)$ file.php?string=$1 [QSA]
But while writing the post, I updated the question's regex to what you see above.
Try:
RewriteCond %{REQUEST_URI} [A-Z] [OR]
RewriteCond %{REQUEST_URI} \.html$
RewriteRule (.*) file.php?string=$1 [QSA]
When using mod_rewrite and you have several URLs to match, it is always better to use RewriteCond to filter and then apply your RewriteRule.
I don't think your problem can be what you think it is: periods in a character class are supposed to mean literal periods, not "any character". If this really is the problem, somehow, then you could change [A-Z\.]+ to ([A-Z]|\.)+; but I doubt it. Some things to try:
what happens if you comment out this line? does that successfully disable this redirect? if not, then obviously the problem isn't with this line. :-)
what happens if you make this a real HTTP redirect, by changing QSA to QSA,R? Does the destination URL look like what you expect? Maybe there are some unexpected periods or uppercase letters? (Warning: this will very likely trigger an infinite redirect loop if you try it in a browser; it'll probably be easier to try submitting the request via port-80 Telnet and seeing the actual HTTP response.)
Also, your rule doesn't quite match how you describe it. For example, your rule wouldn't match a URL like a.b.c, because you only uppercase letters and/or dots to occur in a single "clump"; if they're separated by lowercase letters, no match will occur. Is that just because you didn't want to overcomplicate the description?
I need help writing a simple regex for RewriteRule for mod_rewrite in htaccess. So, here is what I am trying to accomplish:
books/2010-the-world-by-hopkins-139_PPS-1234567
should go to
index.php?pagename=mypage&PPS=1234567&description=2010-the-world-by-hopkins-139
So, in pseducode, the regex has to split the part after books by _ and I should get it into two parts:
PPS (it is always a numeric with 1+ variable number of digits). This is the part after _PPS-
Description (it is always a string). This is the part containing ANYTHING before the _.
I guess the RewriteRule will be something like:
RewriteRule books/(.*)_(.*) index.php?pagename=mypage&PPS=$2&description=$1
But I need correct regex. Plese help.
something like: but flip the $1 and $2 ;)
^books/(.*)_PPS-([0-9]{1,})$