RewriteRule to allow forward slash in query string - php

I have the following re-write rule:
RewriteRule ^([\w-]+)$ modules.php?mod_name=$1
It allows me to open a url like:
mydomain.com/settings
This calls the php file in the following format:
modules.php?mod_name=settings
However, I need to call my php script with mod_name values that contain a path e.g:
modules.php?mod_name=settings/preferences.php?id=1103
I would like the re-write rule to be able to accept the above in the following format:
mydomain.com/settings/preferences/1103
Any ideas how this can be done?

RewriteRule ^([\w-]+)$ modules.php?mod_name=$1
Your \w matches word characters ([a-z] [A-Z] [0-9]) and dashes (-) (thus not the /).
To include the /, you need something like ^([\w-/]+)$

Related

I want to hide URL parameters of my website page by using .htaccess

http://eoflex/CAportal/readmore/?slug=blogtitle
I am trying to hide url parameters by .htaccess file is it possible to show url like this ?
http://eoflex/CAportal/blogtitle
Give me solution I am new to .htaccess file
I am writing code in .htaccess file given below and its not working, i want url like http://eoflex/CAportal/blogtitle I don't want to display /?slug=blogtitle*
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?slug=$1 [L]
you can use the following Code in .htaccess file . you can also use apache rewrite module . By First enabling apache rewrite module in apache server .
Write Below Lines In .htaccess file
RewriteEngine On
RewriteRule ^/CAportal/readmore/([a-zA-Z0-9_-]+)/$ /CAportal/readmore/page.php?slug=$1
HINT
^/read/This/Url/blogtitle /as/This/Url/page.php?slug=blogtitle
INFO
^
Matches the beginning of a string
$
Matches the end of a string
[0-9]
Matches a number, 0–9. [2-4] would match numbers 2 to 4 inclusive.
[a-z]
Matches lowercase letters a–z
[A-Z]
Matches uppercase letters A–Z
[a-z0-9]
Combining some of these, this matches letters a–z and numbers 0–9
Try this (put this in .htaccess in the document root).
RewriteRule ^CAportal/readmore/([a-zA-Z0-9_-]+)/?$ /CAPortal/readmore/?slug=$1 [L]

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 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]

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.

Trouble with URL rewriting in .htaccess

My .htaccess file looks like this:
RewriteEngine On
RewriteRule ^articles/(\d+)*$ ./articles.php?id=$1
So, if the URL foo.com/articles/123 is requested, control is transferred to articles.php?id=123.
However, if the requested URL is:
foo.com/articles/123/
or
foo.com/articles/123/whatever
I get a "404 Not Found" response.
I would like to call articles.php?id=123 in all these cases. So, if the URL starts with foo.com/articles/[digits]... no matter what other characters follow the digits, I would like to execute articles.php?id=[digits]. (The rest of the URL is discarded.)
How do I have to change the regular expression in order to achieve this?
Just don't look for the end:
RewriteRule ^articles/(\d+) ./articles.php?id=$1
You do need to allow the trailing / with:
RewriteRule ^articles/(\d+)/?$
The \d+ will only match decimals. And the $ would disallow matches beyond the end.
If you also need trailing identifiers, then you need to allow them too. Then it might be best to make the match unspecific:
RewriteRule ^articles/(.+)$
Here .+ matches virtually anything.
But if you want to keep the numeric id separate then combine those two options:
RewriteRule ^articles/(\d+)(/.*)?$ ./articles.php?id=$1

Categories