开发者

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

开发者 https://www.devze.com 2023-02-16 13:55 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消