Htaccess mod_rewrite issue - Regex not working correct - regex

just experiencing a huge (for me, the regex newb) problem:
We have a multiste with nested folders, and one line in the htaccess fails in converting to the correct urls:
RewriteRule ^(.+)?/(wp-.*) /$2 [L]
The line should break out everything before the first wp-, and append the rest of the string to the root url. f.e:
/kitchen/wp-includes/js/mediaelement/wp-mediaelement.css
should become
/wp-includes/js/mediaelement/wp-mediaelement.css
but with the above Regex converts to
wp-mediaelement.css
On Urls where there is only one wp-, the thing works. So the error is that apache takes the last occurence of wp- instead of the first.
Long talking:
How can i adjust the above regex to work as expected?
Greets to all!
Edit: Beforte the first wp- can be anything. not only one path. It could also be
/lalelu/loleli/wp-includes

RewriteRule ^(.*?)/(wp-.*) /$2 [L]
should do the trick
edit: http://htaccess.madewithlove.be is a nice tool to quickly debug your rewrite rules

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".

How to redirect from specific subdirectory to a subdomain via .htaccess?

I've been trying to redirect this URL (and all its substructures):
http://example.com/archive/
to (and its corresponding substructures):
http://archive.example.com/
For example: http://example.com/archive/signature/logo.png ==> http://archive.example.com/signature/logo.png
I tried to generate an .htaccess rule using a generator and evaluating it by looking at the regex, which I can understand (I think).
The result was the following rule:
RewriteEngine On
RewriteRule http://example.com/archive/(.*) http://archive.example.com/$1 [R=301,L]
The way I see it, the server will proccess any URL that starts with http://example.com/archive/ , will capture the string that comes next and will change the whole initial portion with the subdomain structure and append the captured string.
Unfortunately, this doesn't seem to work neither on my server, nor on online testing tools such as: http://htaccess.madewithlove.be/
Is there anything I'm missing there?
Thank you!
You should be able to try it this way.
RewriteEngine On
RewriteRule ^archive/(.*)$ http://archive.example.com/$1 [R=301,L]
Note that I did not make it dynamic as you didn't specific if you will have more URL's that need to work this way as well or not.

Rewrite in .htaccess (URL with parameter)

At the moment I'm not really good in unterstanding how URL Rewrite in htaccess works (with regEex).
I want to have following Url:
example.org/lyrik/kategorien/allgemein-1/
It should go to following Url with the number at the at as parameter:
example.org/index.php?seite=kategorienDetail&kategorienId=1
I have tried different ways but they all dont work. The last I have tried was:
RewriteRule ^lyrik/kategorien/allgemein-([0-9]+)\$ index.php?seite=kategorienDetail&kategorienId=$1 [L}
This one made problems on the whole website. every page was inaccessible.
Can someone show me a Rewrite Rule that works in my case and explain the RegEx in it?
I think you have some typos in your rule. Try it this way.
RewriteRule ^lyrik/kategorien/allgemein-([0-9]+)/?$ index.php?seite=kategorienDetail&kategorienId=$1 [L]
Note that I used the ? after the / so that it can be optional. You had a backslash. Also at the end of your rule you had a curly brace instead of the bracket.
EDIT
Based on your comment. Then you can use this if part of the URI is not constant.
RewriteRule ^lyrik/kategorien/([^/]+)-([0-9]+)/?$ index.php?seite=kategorienDetail&kategorienId=$2 [L]
Also this [0-9]+ just means it has to be a number from 0-9 to match the rule and the plus sign means 1 or more basically saying it can't be empty. The * basically means any including a blank(0 or more occurrences). Because your link always ends with a number limiting it to 0-9 in the regex ensures that it won't work if any other character is used there in the URL.
You can see more about regex here.
http://www.regular-expressions.info/tutorial.html
Please try this:
RewriteRule ^lyrik/kategorien/allgemein-(.*?)/$ index.php?seite=kategorienDetail&kategorienId=$1 [QSA,L]
I am writing from the top of my head, but it should work. Let me know if it doesn't, but it should :)
Edit: Please add the QSA flag also to pass any other query parameters you might have.
Alex

Non-working htaccess Rewriterule to match end of URL

I am trying to write an .htaccess rule redirecting all URLs ending in Fizzle/xxxx_showabstract (where xxxx can be an arbitrary string). The rule I wrote is:
RewriteRule .*/Fizzle/(.*)_showabstract$ /abstractdisplay.php?journal=Fizzle&article=$1
But this, simple and straightforward though it to me, doesn't appear to be matching the intended URLs. What am I doing wrong? All help will be profoundly appreciated.
This may work.
RewriteRule ^.*/Fizzle/([^/]*)_showabstract$ /abstractdisplay.php?journal=Fizzle&article=$1 [L]
The issue you're having may be caused by a certain order on executing the regex. But if you use the ^ it will match a complete URL only.

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.