mod rewrite regular expressions with a specific length - php

OK. I have this regular expression in my .htaccess file to rewrite / anything alphanumeric to index.php?id=alphanumeric string.
RewriteEngine on
RewriteRule ^([A-Z-a-z0-9]+)$ index.php?id=$1
the problem i'm having is that when some other variables get added to that string, everything stops working.
For example:
www.someaddress.com/ABCDEFGH works fine.
www.someaddress.com/ABCDEFGH&othervariable=123 fails.
I know my alpha numeric string is always 8 characters. is there a way to make the regular expression only match 8 and leave the rest of the string?
Thanks in advance.

Something like that should do the trick. You want to remove the $ to let it ignore the rest of the variables.
RewriteRule ^([A-Z-a-z0-9]{8}) index.php?id=$1

try this:
RewriteRule ^([A-Za-z0-9]{8}) index.php?id=$1
Please note the - character between Z and a is not required

Related

How to handle special characters like & and / in .htaccess rules?

In my .htaccess file I have defined following rule,
RewriteRule ^([-0-9a-zA-Z]+) search.php?id=$1
The above rule works fine if I am browsing http://example.com/abcd
I need to use the symbols & % - / in the url like: http://example.com/ab&cd
What changes have to be made to the rule for this to work?
No idea how that rule is working for you. First, it loops. Second, there is no capture groups for $2 and $3, but it doesn't matter because $1 is always "search" anyways. I'm assuming you've pasted a partial snippet of a rule that you have that works.
The reason why &, %, or / isn't being matched is because your regex says:
[-0-9a-zA-Z]+
which means: one or more letters, numbers, or a dash. So no &, %, or /. So you can add those into the square brackets:
RewriteRule ^([-0-9a-zA-Z/%&]+) search.php?id=$1&ff=$2&ffid=$3
However, keep in mind that the URI is decoded before any rules get applied. This means if the URI looks like:
/foo%28bar
You don't need to match against %, because the URI gets decoded into:
/foo(bar
and you need to match against (. A better option may to just match against every except dots:
RewriteRule ^([^.]+) search.php?id=$1&ff=$2&ffid=$3
or whatever you don't want in your match.
Try:
RewriteRule ^([^.]+)$ search.php?id=$1 [B]
The difference here is the $ to bound the match to the end of the URI, and the B flag ensures the & gets encoded.
You need to URL encode 'ab&cd' using urlencode
so that & gets turned into %26.
After this, in search.php you'll need to account for it and decode it, using urldecode.
Do the URL like this:
http://example.com/id/ff/ffid
and write your rule around that. Also, why do you need 3 ID parameters? Couldn't you just lookup the other 2 using the first one?
Do with your url
RewriteRule ^directory/([^/.]+)$ /searchpage.php?search_keywords=$1 [L]

mod_rewrite code check double rewrite rule

Here is my current config:
1.RewriteRule ^india/news.php /portal/india/stories/telugu.php??topicid=14
2.RewriteRule ^india/telugu.php?currentpage=([0-9]+).&topicid=14 /portal/india/stories/telugu.php?currentpage=$1&topicid=14
Rule 1 rewrites http://www.domain.com/portal/india/stories/telugu.php?topicid=14 to
http://www.domain.com/india/news.php which works fine.
Rule 2 should rewrite http://www.domain.com/portal/india/stories/telugu.php?currentpage=p.no&topicid=14 to http://www.domain.com/india/telugu.php?currentpage=p.no&topicid=14 which doesn't work.
It shows a 404 not found.
Here p.no = numeric number
If I need to elaborate my problem, please let me know.
RewriteRule uses regular expressions so having a ? in the regular expressions is not denoting a query string but instead kicking off as a regular expressions key.
Also RewriteRule really only looks at the request URI and ignores the query string unless you set a conditional for the query string.
so Rule 2 should be something like this
RewriteCond %{QUERY_STRING} currentpage=([0-9]+).&topicid=14
RewriteRule ^india/telugu.php /portal/india/stories/telugu.php?currentpage=%1&topicid=14
Please note that in this setup you need %1 instead of $1
I hope this helps I have had this issue several times and this is the only way I ever get it to work. Also you can test rewrites before you do them on sites like http://martinmelin.se/rewrite-rule-tester/

mod_rewrite RewriteRule for abc.php?a=1&b=2 to abc/2.html

I am a real newbie to the either mod_rewrite or Regex.
Therefore I just need your help for the following problem.
I got a PHP-Page that looks just like:
stuff.php?id=1&text=2
I know want to to look like
stuff/2.html
Do anyone of you have the RewriteRule line for the htaccess in mind to let it look just like this?
Thanks a lot in advance!
A rewrite rule for this particular page:
RewriteRule ^stuff/2\.html$ stuff.php?id=1&text=2
And if 2 should be dynamic:
RewriteRule ^stuff/([0-9]+)\.html stuff.php?id=1&text=$1
A little explanation:
^ and $ stand for start and end of the string, so we don't match longstuff/2.html.php.
The dot has to be escaped \. because otherwise it has a special meaning in RegEx ("any character")
the parantheses in the second pattern are a "capture group", their content will be available in the rewrite as $n (with n = number of capture group, in this case 1)
[0-9] is a character class, matches one character of the class, in this case a digit
+ means "one or more"
Here's a rule to redirect stuff/2.html to stuff.php?id=1&text=2
RewriteRule ^stuff/([\d]+)\.html$ stuff.php?id=1&text=$1 [L]
Notice [\d]+ will only accept numbers, if you want to allow letters and caret, use the following rule :
RewriteRule ^stuff/([\w-]+)\.html$ stuff.php?id=1&text=$1 [L]

Apache RewriteRule rule

Alright, I give up. I just can't quite wrap my mind around apache rewrites, I've looked through a lot of the stackoverflow suggestions and none seems to make sense to me.
So, I have a script that current renders content based on www.example.com/index.php?article=some-article-name
But, I want the user to think that page is www.example.com/section/some-article-name
I've tried using stuff like
# Turn on URL rewriting
RewriteEngine On
RewriteRule ^/section/([a-zA-Z0-9\-]+)$ index.php?article=$1
I discovered the answer thanks to the direction of all of these folks.
RewriteRule ^section/([a-zA-Z0-9]+)$ test.php?article=$1
RewriteRule ^section/([a-zA-Z0-9]+)/$ test.php?article=$1
You need both to handle 2 different types of requests, ones with a / at the end and those that don't.
You may want a simpler rule like.
RewriteRule ^/section/(.*) index.php?article=$1
A name like some-article-name will fail because you won't match the hyphen. If you want a limited regex try something like:
RewriteRule ^/section/([-_.a-zA-Z0-9]+)$ index.php?article=$1
This will match ASCII alphanumeric characters along with punctuation most likley to be in the name.
Either of these rules will fail if you have parameters on the incoming request.
Your RegEx is SO close. You have a capturing group ([a-zA-Z0-9]+) that is looking for one or more lower case letters, upper case letters and/or numbers, but what it isn't looking for is a hyphen. Try this:
RewriteRule ^/section/([a-zA-Z0-9\-]+)$ index.php?article=$1
Because the hyphen has special significance in Regular Expressions you need to escape it.

URL Rewrite with htaccess and PHP

I have a URL: search/?word=asdf and want to redirect to: search/word/asdf/ and running internally: ?cmd=search&word=asdf
This so you can get the PHP $ _GET ['cmd'] and $ _GET ['word'].
How to do it in htaccess?
EDIT:
My .htaccess now is:
RewriteRule search(.*) %{HTTP_REFERER}cmd/search$1
RewriteRule cmd/search/?key-word=(.*) %{HTTP_REFERER}cmd/search/key-word/$1
But this not working. The new URL ever is:
localhost/bruc/sandbox/electrolux/trunk/cmd/search/?key-word=asdf
But it should be: localhost/bruc/sandbox/electrolux/trunk/cmd/search/key-word/asdf
So, I redirect this correct URL to: localhost/bruc/sandbox/electrolux/trunk/?cmd=search&key-word=asdf
But not working fine! Try, my approach here: http://htaccess.madewithlove.be/
Try RewriteRule ^([^/]*)/word/([^/]*)$ /?cmd=$1&word=$2 [L]. I believe that will accomplish your goal.
Try this :
RewriteEngine on
RewriteRule ^search/word/(.*)$ /?cmd=search&word=$1 [L]
Check this.
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)/([^/]+) /?cmd=$1&word=$2 [L]
There are three parts to this:
RewriteRule specifies that this is a rule for rewriting (as opposed to a condition or some other directive). The command is to rewrite part 2 into part 3.
This part is a regex, and the rule will be run only if the URL matches this regex. In this case, it says - look for the beginning of the string, then a bunch of non-slash characters, then a slash, then another bunch of non-slash characters. then again bunch of non-slash characters, then a slash, then another bunch of non-slash characters. The parentheses mean the parts within the parentheses will be stored for future reference.
Finally, this part says to rewrite the given URL in this format. $1 and $2 refer to the parts that were captured and stored.

Categories