Apache2: Mod_rewrite wildcard text.html - regex

We have changed e-commerce vendors and I need to perserve some of the SEO we've done.
I want to do a 301 for a set of pages to a single new URL
I have a set of pages that end all with the same tickets.htm. So, for example, I have a pages like /blank_tickets.htm and /concert_tickets.htm and the list goes on.
So I tried this:
RewriteRule ^/(.*)tickets.htm$ /t/tickets/types/standard
I tried variations of this no leading / no $, etc.
I'm sure I'm missing something simple but my Google-fu is not returning a relevant example.
Thanks!

You didn't escape the dot. Use \. in your regular expression at the .htm part.
Edit: also use the [R=301] flag, or simply [R] to produce a HTTP 301 header.

Related

htaccess regex variable parameter

I'm not used to regex and figure I've lost too many hours trying to resolve this, so thought I'd ask for help. I am trying to prettify the html extension.
My site will use URLs that have variable parameters. For example:
mysite.com/article/this-is-an-entry
mysite.com/article/this-is-an-entirely-different-entry
All will use .html as the extension.
In the htaccess file, I have tried
RewriteRule ^(article\/[a-z].*)$ $1.html [NC,L]
as well as slight variations of this, but cannot get this right. Thanks in advance for any assistance.
Firstly, let's look at the regex you have:
^(article/[a-z].*)$
This matches exactly the string "article/", followed by at least one letter (case insensitive due to the NC flag), followed by zero or more of anything. It's quite broad, but should match the examples you gave.
One way to test that it's matching is to add the R=temp flag to the rule, which tells Apache to redirect the browser to the new URL (I recommend using "=temp" to stop the browser caching the redirect and making later testing harder). You can then observe (e.g. in your browser's F12 debug console) the original request and the redirected one.
RewriteRule ^(article/[a-z].*)$ $1.html [NC,L,R=temp]
However, as CBroe points out, your rule will match again on the target URL, so you need to prevent that. A simple way would be to use the END flag instead of L:
Using the [END] flag terminates not only the current round of rewrite processing (like [L]) but also prevents any subsequent rewrite processing from occurring in per-directory (htaccess) context.
So:
RewriteRule ^(article/[a-z].*)$ $1.html [NC,END]
Alternatively, you can make your pattern stricter, such as changing the . ("anything") to [^.] ("anything other than a dot"):
^(article/[a-z][^.]*)$
To be even more specific, you can add a RewriteCond with an extra pattern to not apply the rule to, such as "anything ending .html".

Rewriting URL with special characters

I have a bunch of crawl errors on my site to pages that don't exist (and never existed). It was created from some bad code that generated JSON-LD schema.org for a search page. Basically, I have thousands of 404s that look like this
http://www.domain.com/search/%7Bsearch_term%7D%2Fpage%2F2%2Fpage%2F3%2Fpage%2F6%2Fpage%2F2%2Fpage%2F3%2Fpage%2F6%2Fpage%2F6%2Fpage%2F2%2Fpage%2F2%2Fpage%2F3%2Fpage%2F2%2Fpage%2F6%2Fpage%2F3%2Fpage%2F2%2Fpage%2F6%2Fpage%2F3%2Fpage%2F2%2Fpage%2F7%2Fpage%2F2%2Fpage%2F2%2Fpage%2F8%2Fpage%2F3
http://www.domain.com/search/%7Bsearch_term%7D%2Fpage%2F2%2Fpage%2F6%2Fpage%2F2%2Fpage%2F3%2Fpage%2F6%2Fpage%2F2%2Fpage%2F6%2Fpage%2F6%2Fpage%2F6%2Fpage%2F2%2Fpage%2F3%2Fpage%2F3%2Fpage%2F3%2Fpage%2F2%2Fpage%2F2%2Fpage%2F3%2Fpage%2F7%2Fpage%2F2%2Fpage%2F7%2Fpage%2F2%2Fpage%2F8%2Fpage%2F3
I am terrible with regex, and could use some help on figuring out how to resolve this. As a short term solution, I just want to redirect URL requests with /search/{search_term}/ in the URL to just the /search page.
Any tips on what I should be doing? This is what I have been messing around with which is obviously wrong. Sorry if this is terribly easy question, but I've been trying different things I find online, and have just beating my head without success.
RewriteRule ^/search/%7Bsearch_term%7D$ /search [R,L=301]
RewriteRule ^/search/\{search_term\}$ /search [R,L=301]
You're pretty close. You can use this rule:
RewriteRule ^search/\{search_term\} /search [R,L=302,NC]
Or if search_term is also a dynamic string then use:
RewriteRule ^search/\{[^}]+\} /search [R,L=302,NC]
EDIT: You will need this directive in your Apache or vhost config:
AllowEncodedSlashes On
otherwise Apache rejects requests with these special characters without giving you any chance to handle them in mod_rewrite.
Just off the top of my head, this should do it:
RewriteRule ^search\/(.+) http://www.yourdomain.com/search [R=302]
Explanation of the syntax above:
^ indicates the start of the match
\ is the escape character, so \/ means escape the forward slash (probably unnecessary, but does no harm)
() is a capture group
. means any character
+ means one or more of
So the whole regex means:
starting at the current position in the folder hierarchy, match
search/
followed by one or more characters.
N.B. Importantly, it is the R flag which indicates the type of redirect, so you need R=302, (not L=302, which doesn't exist)

Regex for matching a pattern at the end of a URL

I have a really weird pattern that sometimes shows up at the end of urls and I'm trying to write an apache rewrite rule to get rid of it.
Meaning, I'm trying to formulate a regex expression to match anything ending with that pattern
So I might have something like
mysite.com/blah#_=_
And I want to match that pattern after blah but I think I either need to escape it or do something. I currently have these sets of rules, well, really loosely laid out
RewriteEngine on
RewriteCond %{HTTP_HOST} regex-goes-here [NC]
RewriteRule ^/(.*) replace-with-url-minus-pattern [R]
You can't use mod_rewrite or anything on the server's end for to get rid of that. The stuff starting with the # is called a URL Fragment, and it's something browsers (and javascript and other stuff that's client side) use to determine how to display or render content, or used by scripts to pass bits of data between each other. The #_=_ portion of the URI is never even sent to the server.
You'll need to do something on the browser's end, maybe something like this: Remove fragment in URL with JavaScript w/out causing page reload

What's wrong with this regular expression in a .htaccess file?

I'm trying to understand why this regular expression isn't working in my .htaccess file. I want it so whenever a user goes to the job_wanted.php?jid=ID, they will be taken to job/ID.
What's wrong with this?
RewriteEngine On
RewriteCond %{QUERY_STRING} jid=([0-9]+)
RewriteRule ^job_wanted\.php?$ job/%1? [R]
I want it so when a user clicks on http://localhost/jobwehave.co.za/jobs/ID they are shown the same results as what below would show http://localhost/jobwehave.co.za/jobs?id=ID.
Sorry for the mix up. I still very confused to how this works.
The primary problem is that you can't match the query string as part of RewriteRule. You need to move that part into a RewriteCond statement.
RewriteEngine On
RewriteCond %{QUERY_STRING} jid=([0-9]+)
RewriteRule ^job_wanted\.php$ /job/%1?
Editing to reflect your updated question, which is the opposite of what I've shown here. For the reverse, to convert /job/123 into something your PHP script can consume, you'll want:
RewriteEngine On
RewriteRule ^/job/([0-9]+)$ /path/to/job_wanted.php?jid=$1
But you're probably going to have trouble putting this in an .htaccess file anywhere except the root, and maybe even there. If it works at the root, you'll likely need to strip the leading / from the RewriteRule I show here.
Second edit to reflect your comment: I think what you want is complicated, but this might work:
RewriteEngine On
RewriteRule ^/job/([0-9]+)$ /path/to/job_wanted.php?jid=$1 [L]
RewriteCond %{QUERY_STRING} jid=([0-9]+)
RewriteRule ^job_wanted\.php$ http://host.name/job/%1? [R]
Your fundamental problem is that you want to "fix" existing links, presumably out of your control. In order to change the URL in the browser address bar, you must redirect the browser. There is no other way to do it.
That's what the second cond+rule does: it matches incoming old URLs and redirects to your pretty URL format. This either needs to go in a VirtualHost configuration block or in the .htaccess file in the same directory as your PHP script.
The first rule does the opposite: it converts the pretty URL back into something that Apache can use, but it does so using an internal sub-request that hopefully will not trigger another round of rewriting. If it does, you have an infinite loop. If it works, this will invoke your PHP script with a query string parameter for the job ID and your page will work as it has all along. Note that because this rule assumes a different, probably non-existent file system path, it must go in a VirtualHost block or in the .htaccess file at your site root, i.e. a different location.
Spreading the configuration around different places sounds like a recipe for future problems to me and I don't recommend it. I think you'll be better off to change the links under your control to the pretty versions and not worry about other links.
The ^ anchors the regex at the beginning of the string.
RewriteRule matches the URI beginning with a / (unless it's in some per-directory configuration area).
Either prefix the / or remove the anchor ^ (depending on what you want to achieve)
You haven't captured the job ID in the regex, so you can't reference it in the rewritten URL. Something like this (not tested, caveat emptor, may cause gastric distress, etc.):
RewriteRule ^job/([0-9]+) job_wanted.php?jid=$1
See Start Rewriting for a tutorial on this.
You need to escape the ? and . marks if you want those to be literals.
^job_wanted\.php\?jid=9\?$
But although that explains why your pattern isn't matching, it doesn't address the issue of your URL rewriting. I'm also not sure why you want the ^ and $ are there, since that will prevent it from matching most URLs (e.g. http://www.yoursite.com/job_wanted.php?jid=9 won't work because it doesn't start with job_wanted.php).
I don't know htaccess well, so I can only address the regex portion of your question. In traditional regex syntax, you'd be looking for something like this:
s/job_wanted\.php\?jid=(\d*)/job\/$1/i
Hope that helps.
Did you try to escape special characters (like ?)?
The ? and . characters have a special meaning in regular expressions. You probably just need to escape them.
Also, you need to capture the jid value and use it in the rule.
Try to change your rules to this:
RewriteEngine On
RewriteRule ^job_wanted\.php\?jid=([0-9]+)$ /job/$1
Something like
ReWriteRule ^job\_wanted\.php\?jid\=([0-9-]+)$ /job/$1
should do the trick.

mod_rewrite: replace underscores with dashes

I'm revealing my embarrassing ignorance of REGEX-fu here, but: I currently have a website where a load of the articles' URLs are written as "article_name", whilst the newer ones are written as "article-name".
I want to move all of them to using dashes, so is there a regular expression I could use to rewrite the older URLs to their newer equivalents?
Thanking you in advance!
First you must achieve consistency in the existing URLs. Basically, you have to normalize all existing names to always use dashes. Ok, you've done that.
We're starting with the following assumption:
The URL is roughly of the form:
http://example.com/articles/what-ever/really-doesnt_matter/faulty_article_name
where only URLs under /articles should be rewritten, and only the /faulty_article_name part needs to be sanitized.
Greatly updated, with something that actually works
For Apache:
RewriteEngine On
RewriteRule ^(/?articles/.*/[^/]*?)_([^/]*?_[^/]*)$ $1-$2 [N]
RewriteRule ^(/?articles/.*/[^/]*?)_([^/_]*)$ $1-$2 [R=301]
That's generally inspired by GApple's answer.
The first /? ensures that this code will run on both vhost confs and .htaccess files. The latter does not expect a leading slash.
I then add the articles/ part to ensure that the rules only apply for URLs within /articles.
Then, while we have at least two underscores in the URL, we keep looping through the rules. When we end up with only one remaining underscore, the second rule kicks in, replaces it with a dash, and does a permanent redirect.
Phew.
Try this:
RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]
The first rule replaces one underscore at a time until there are one or less left. The last rule will then replace the last underscrore and do an external redirect.
A potential different approach to think about:
I'm assuming that your "old format" and your "new format" will be in different directories for this idea, if they aren't you might want to consider making the new format have a different directory name.
For instance:
http://site.com/articles/2008/12/31/new_years_celebration
http://site.com/article/2008/12/31/new-years-celebration
In which case you could use mod_rewrite to detect anything in the "old directory" and redirect it to a "redirector.php".
Although on second thought, your mod_rewrite could look for something like this:
RedirectRule /articles/(.*_.*) /redirector.php?article=$1
Matching anything with a _ and sending it through the redirector.
Inside of redirector.php you can get the $_SERVER['REQUEST_URI'] and use tools like preg_replace and even database queries to find the correct url to redirect them to - as well as study the number of hits to old urls.
How will mod rewrite know what the actual url is supposed to be? You can rewrite all articles to use the underscore or the dash, but there is no way for mod_rewrite to tell if new location exists.
For example,
/I_Like_Bees is stored as /path/i_like_bees
/I-like-flowers is stored as /path/i-like-flowers
You want i-like-bees to rewrite to i_like_bees.
If you rewrite underscores to dashes, i_like_bees wouldn't be found
if you rewrite dashes to underscores i-like-flowers wouldn't be found
If you stored all your articles consistently you could easily make a rewrite rule work. Instead you probably have to write a script to check the directories existence and do a 301 redirect to the correct place.
Here's a method: http://yoast.com/apache-rewrite-dash-underscore/
Basically it separates the url into tokens on either side of the underscore, and rewrites the tokens again with the underscore replaced. The problem is it only replaces a single underscore at a time; it will redirect to a closer but not quite correct url, which will again redirect to a even closer, but possibly still not correct url...
It suggests fixing the multiple redirects by having several rewrite conditions & rules with successively more underscores and tokens, but this would require as many conditions and rules as you have underscores in your longest title.
Make sure to add any qualifiers if you can however, as the rule may replace paths you don't want changed (eg., image files) as is.