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
Related
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".
I have a rewrite rule (in an Apache htaccess file) which is attempting to use a back reference twice from just one capture ($1):
RewriteRule ^([A-Za-z0-9_-]+)/?$ $1.php?nav=$1
It appears that the query string is being left emtpy, like
example.com/new
is being re-written as
example.com/new.php?nav=
what I want is
example.com/new.php?nav=new
My question: can I reference $1 twice in the expression?
UPDATE:
The Apache documentation on mod_rewrite indicates that you can reference a capture as many times as you like in the substitution part of a rewrite rule. However, after trying for a couple of days I was not able to make it work. I did get my rule to pass in the online regex testers that are out there, but not on my site. In the end I re-designed my menu system so that I could use simpler rewrite rules.
This regex that your're using is wrong:
^(A-Za-z0-9-_)$
Range is allowed in square brackets only and your need to use + accessor to match more than 1 character.
Replace your RewriteRule with this:
RewriteRule ^([a-z0-9_-]+)/?$ $1.php?nav=$1 [L,NC,QSA]
I've been working out an htaccess redirect directive for (example) an online shop.
Products are displayed on product.php, with an ID parameter passed in.
What I'm aiming for is to have
products/someValue/someID
map to
product.php?param1=someValue¶m2=someID
Here's where I'm at so far. This mostly works, but a couple of things are not quite what I want. I need it to basically separate everything after products/ by forward slashes and use each of those as a parameter, regardless of how many or few. also, a trailing slash is optional - my rule throws a 404 if it's not included. Can anyone point me in the right direction?
RewriteRule ^products/(.*)/(.*)/.*$ /product.php?param1=$1¶m2=$2
Your current regex is greedy. Quantifiers are greedy by default. Adding a ? makes them ungreedy. Or you can change your regex to match anything except / Either of these should work:
RewriteRule ^products/(.*?)/(.*?)(/.*)?$ /product.php?param1=$1¶m2=$2
RewriteRule ^products/([^/]*)/([^/]*)(/.*)?$ /product.php?param1=$1¶m2=$2
Read up on greediness in regexes to understand this better.
Try changing the regex to non-greedy:
RewriteRule ^products/(.*?)/(.*?)/.*$ /product.php?param1=$1¶m2=$2
or:
RewriteRule ^products/([^/]+)/([^/]+)/.*$ /product.php?param1=$1¶m2=$2
I am working on some SEO for my site, and using urls like category/this-cat.html and category/this-cat-p2.html to redirect to index.php?mode=viewCat&id=this-cat and index.php?mode=viewCat&id=this-cat&start=10 respectively.
The problem is that my RewriteRule needs a conditional to check if the -p2 part is present in the URL, and then return either 0 or the digit after p.
Current rule:
RewriteRule ^category/(.*?)\.html$ index.php?mode=viewCat&title=$1
I would have thought that the correct syntax for this would have been:
RewriteRule ^category/(.*?)(?(-p)(.*?)|0)\.html$ index.php?mode=viewCat&title=$1&start=$2
however, this causes the server to return a 500 error.
Even after having read tutorials and worked with them for the past week, I still have little grasp on them. Can anyone explain how to make a conditional like this work?
You're close. I believe what you are trying to do is not capture the optional -p# grouping, but want the digit if it exists. The non-capture flag for a group is a ?: prefix.
RewriteRule ^category/(.*?)(?:-p(\d+))?\.html$ index.php?mode=viewCat&title=$1&start=$2
Note: I used \d (digit) as it's better to be specific about what you are matching. Also start will have a digit or nothing. Your server-side code is better suited to handle the rest of the logic you described.
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.