I have this .htaccess RewriteRules, that doesn't work.
RewriteRule ^(.+)\/(.+)\/$ /index.php?pg=$1&act=$2
What this code should do, is transofrm /somthing/other/ into /index.php?pg=somthing&act=other.
Making some test, I discovered that also the pg var is not passed.
I have another line like this, RewriteRule ^(.+)\/$ /index.php?pg=$1 an it work! So, I don't know why the first one didn't work!!
If you don't need to use any other characters than a-z and 0-9 I recommend you to use this:
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)$ /index.php?pg=$1&act=$2
It should work.
Related
I have this code in a .htaccess file..
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteRule ^(.+)-headquarters-([0-9]+)\.html company.php?lid=$2
Now my question is, what happens if the matching result from the first parentheses contains a hyphen? There is a very good chance a lot of them will. Is this where the ungreedy modifiers come in!?
I don't think in this case you need to make it ungreedy. e.g. test-headquarters-headquarters-42.html will match perfectly fine (with test-headquarters being $1).
But for completeness I will suggest a few options
You can make it ungreedy by adding a ? after it. So your rule would become (although, it this case, there would not be any case where it would result in different behaviour):
RewriteRule ^(.+)?-headquarters-([0-9]+)\.html company.php?lid=$2
You could also choose match anything except a hyphen by using
RewriteRule ^([^-]+)-headquarters-([0-9]+)\.html company.php?lid=$2
I have been using this rule:
RewriteRule ^([^\.]+)/?$ index.php?page=$1 [L]
Which will turn
example.com/page/hello
...into
example.com/index.php?page/hello
However, if I have a file extension on the end of that, say,
example.com/page/hello/doc.html
...it doesn't work. How can I make that URL become:
example.com/index.php?page/hello/doc.html
...and
example.com/page/
...become:
example.com/index.php?page/
Thank you very much if you are able to help with this. I've tried fiddling with the regular expression, but I just can't make it work.
Thanks.
This section of your regex will match one or more characters that are anything except a full stop:
[^\.]+
If you want to match one or more of any character, then just write:
.+
instead
I know there are other questions that are similar to this.. but I'm really struggling with mod_rewrite syntax so I could use some help.
Basically, what I am trying to do is have the following redirect occur:
domain.com/1/ redirect to domain.com/?id=$1 (also should work for www.domain.com)
What I have so far (that isn't working):
RewriteEngine On
ReRewriteRule ^/([0-9])$ /?id=$1
A few issues.
First is terminology: if you want when a user types domain.com/1/ that the request is served by index.php?id=1, then you are rewriting /1/ to index.php?id=1, not the other way around as you said.
Second, simple typo: RewriteRule, not ReRewriteRule.
Second, [0-9] is the right way to match a number, but it'll only match a single digit. If you want to handle /13 then you should match one or more instances [0-9] by writing [0-9]+.
Third, the target of your rule should be the file you want to serve. / is not a file or an absolute URL, write out the index.php if that's what you mean.
Third, you say you want to handle /1/, but your rule says that the matched request must end in a number, not a slash. If you want to accept the slash whether it's there or not, put that in the rule.
RewriteRule ^/?([0-9]+)/?$ index.php?id=$1 [L]
Does that work?
You've three issues:
RewriteRule is misspelt as point out by Michael, you need to worry about the trailing slash, and you need to stop processing rules when you've found the match:
RewriteRule ^/(\d+)/?$ /?id=$1 [L]
You have misspelled RewriteRule. Otherwise, I think your syntax looks correct.
RewriteEngine On
ReRewriteRule ^/([A-Za-z0-9]+)$ /?id=$1
--^^^---------
Actually, you should probably remove the /:
RewriteEngine On
RewriteRule ^([A-Za-z0-9$_.+!*'(),-]+)$ /?id=$1
------^^^---------
EDIT Added the +. Look at all the answers here. You need a composite of them., including the + and the [L] in addition to what I have here.
EDIT2 Also edited to include alpha characters in the id.
EDIT3 Added special characters to regex. These should be valid in a URL, but it's unusual to find them there.
Even simple .htaccess gives me headaches and I need to do the following generic mapping:
http://example.com/project/controllername/key1/val1/key2/val2/.../keyN/valN
-->
http://example.com/project/controllername.xyz?key1=val1&key2=val2...&keyN=valN
example:
http://example.com/so/pagecontroller/id/1/time/12345/title/helloworld
-->
http://example.com/so/pagecontroller.xyz?id=1&time=12345&title=helloworld
Any guidance will help! Especially with handling special chars like '/', '?' and '&' (more?) in keys and values.
EDIT: To clarify, 'project' and 'controllername' paths are dynamic - they are not static. Also the number of keys and values is not pre-determined! I need help in creating the htaccess file code and where to place this file in the web tree and if apache needs restarting everytime the htaccess file is modified. Thanks!
Try these rules:
RewriteRule ^([^/]+/[^/]+)/([^/]+)/([^/]+)(/.+)?$ $1$4?$2=$3 [QSA,N]
RewriteCond $1 !.+\.xyz$
RewriteRule ^([^/]+/[^/]+)$ $1.xyz [L]
As long as you don't want to select those special characters, they should be no problem. A rule for your example might be:
RewriteRule http://example.com/so/pagecontroller/id/([0-9]+)/time/([0-9]+)/title/(.*)$ http://example.com/so/pagecontroller.xyz?id=$1&time=$2&title=$3
I don't think you can have a dynamic number of variables in a rewrite rule. But what you can do is the following:
RewriteRule http://example.com/so/pagecontroler/(.*) http://example.com/so/pagecontroler.xyz?vars=$1
Than you have a GET parameter with the name "vars" and the rest of the query as a value. You can than split the different keys and values server side e.g. with the explode() function of PHP.
I have a problem that I cannot wrap my head around.
I'm using Apache and PHP
I need to get:
http://localhost.com/cat_ap.php?nid=5964
from
http://localhost.com/cat_ap~nid~5964.htm
How do I go about changing that around? I have done more simple mod rewrites but this is slightly more complicated. Can anyone give me a leg up or point me in the right direction
RewriteRule ^/cat_ap~nid~(.*)\.htm$ /cat_ap?nid=$1 [R]
The [R] at the end is optional. If you omit it, Apache won't redirect your users (it will still serve the correct page).
If the nid part is also a variable, you can try this:
RewriteRule ^/cat_ap~([^~]+)~(.*)\.htm$ /cat_ap?$1=$2 [R]
EDIT: As Ben Blank said in his comment, you might want to restrict the set of valid URLs. For example, you might want to make sure a nid exists, and that it's numerical:
RewriteRule ^/cat_ap~nid~([0-9]+)\.htm$ /cat_ap?nid=$1
or if the nid part is a variable, that it only consists of alphabetical characters:
RewriteRule ^/cat_ap~([A-Za-z]+)~([0-9]+)\.htm$ /cat_ap?$1=$2
Assuming the variable parts here are the "nid" and the 5964, you can do:
RewriteRule ^/cat_ap~(.+)~(.+).htm$ ^/cat_ap?$1=$2
The first "(.+)" matches "nid" and the second matches "5964".
If you want everything arbitrary:
RewriteRule ^/(\w+)~(\w+)~(\w+)\.htm$ $1?$2=$3 [L]
Where \w is equal to [A-Za-z0-9_]. And if you want to use this rule in a .htaccess file, remove the leading / from the pattern.