I would like to make a rewrite rule to redirect to what the user has typed right after the main url, so no folder (like www.site.com/user1), I think that would mean to access $0 which doesn't work...
The rule I'm trying to use is:
RewriteRule ^([0-9a-zA-Z]+) show-user.php?user=$0
But it does not work...
Any ideas?
Ok, new file and it's still wrong...
Full .htaccess file:
DirectoryIndex index.php
RewriteEngine on
RewriteRule ^([0-9a-zA-Z]+) test.php?var1=$1 [L]
full test.php file:
<?php
print_r($_GET);
?>
Ouput for domain/user1
Array ( [var1] => test )
It makes no sense :(
How can I make sure in php var1 will be user1 (as it should be) in the example above? I think the rewrite rule is wrong somehow...
The parameters are not 0-indexed
RewriteRule ^([0-9a-zA-Z]+) show-user.php?user=$1
Related
so I'm trying to make a pretty URL using htaccess
I want to convert www.domain.com/r.php?id=123 to www.domain.com/r/123
I used this htaccess code to do it :-
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^r/([0-9]*)/([a-z]*)$ ./r.php?user=$1
However, on r.php, when I try to get the value of ID..I get
/Library/WebServer/Documents/ico/main/r.php:2:
array (size=0)
empty
Here;s my r.php file
<?php
var_dump($_REQUEST);
?>
What am I doing wrong in here?
If your link contains id then make sure in your .htaccess rewrite rule you are actually using id. So just follow this logic:
RewriteEngine On
RewriteRule whatever/r/(.*)script_name.php?id=$1 [L]
For this page test.php
i have to use 2 format url
www.example.com/test/1234
and
www.example.com/test/1234/abcd
My .htaccess is
RewriteRule ^test/([^-]*)/([^-]*)$ /test.php?one=$1&two=$2 [L]
It's work good with www.example.com/test/1234/abcd but redirect internal error with www.example.com/test/1234
I want to know how can i edit .htaccess for work good with my 2 url format on test.php ?
Try the following instead:
RewriteRule ^test/([^-]*)$ /test.php?one=$1&two=$2 [L]
Just remove the 2nd /([^-]*) it is making no difference, as you can see here. Removing it will allow for you to access www.example.com/test/1234 too.
You can use:
RewriteRule ^test/([^/-]+)(?:/([^-]*))?$ /test.php?one=$1&two=$2 [L]
today i tried to create .htaccess file that replacing ? and = with / ,
test.php code:
<?php
echo $_GET['myparam'];
?>
.htaccess:
i used this writerule:
RewriteEngine On
RewriteBase /
RewriteRule ^test/myparam/([0-9]+)/?$ test.php?myparam=$1 [NC,QSA,L]
ok, i navigated to www.web.com/test.php?myparam=123
there is no redirect to www.web.com/test/myparam/123
so navigated to: www.web.com/test/myparam/123 (Manually) and the php script is worked,
i changed the myparam value to abc instead of 123 : www.web.com/test/myparam/abc
and then it redirects to 404 not found page...(the server don't know that abc is not directory when integer works when string 404)
!so what i want to do:!
www.web.com/ test .php? inttParam = 1 & strrParam = stringhere & p = 1
TO
www.web.com/ test/inttParam/1/strrParam/stringhere/p/1
and when i use $_GET['p'] it will work.
i changed the myparam value to abc instead of 123 and it didn't work
Well of course it won't work since your rule is matching only numbers in the end:
RewriteRule ^test/myparam/([0-9]+)/?$ test.php?myparam=$1 [NC,QSA,L]
change your rule to this to make it work with anything:
RewriteRule ^test/myparam/([^/]+)/?$ test.php?myparam=$1 [NC,QSA,L]
To make it recursion based generic rule to convert /test/inttParam/1/strrParam/stringhere/p/2 to /test.php?p=2&strrParam=stringhere&inttParam=1`:
RewriteRule "^(test)(?:\.php)?/([^/]+)/([^/]*)(/.*)?$" /$1.php$4?$2=$3 [NC,L,QSA]
RewriteEngine on
# do not affect files
RewriteCond %{REQUEST_URI} !(\..{2,4})$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ index.php?parameter1=$1&%1 [L]
Something like that perhaps?
ok, i navigated to www.web.com/test.php?myparam=123
there is no redirect to www.web.com/test/myparam/123
I don't know if that means that you also want to make a redirection (presumably 301) from the one with GET params to the one with slashes.
If so, I would recommend to use the previous answer from anubhava and handle any 301 redirection with PHP. Actually, you can redirect with "RewriteRule" using the "R" flag, but I admit that I woundn't know how to solve this particular case.
Anyway, I think there is no need, unless old URLs with GET params were working well at SEO and you didn't wan't to lose ranking.
This is probably a very easy question. Anyway how do you use variables from a url without requests. For example:
www.mysite.com/get.php/id/123
Then the page retrieves id 123 from a database.
How is this done? Thanks in advance!
UPDATE
If i have the following structure:
support/
sys/
issue/
issue.php
.htaccess
home.php
etc.....
With .htaccess file containing:
RewriteEngine On
RewriteRule ^/issue/(.*)$ /issue/issue.php?id=$1 [L]
Why do I have to type:
http://www.mysite.com/support/sys/issue/issue/1234
In order to load a file? When I want to type
http://www.mysite.com/support/sys/issue/1234
also, how do I then retrieve the id once the file loads?
Problem
This is a very basic/common problem which stems from the fact that your .htaccess rule is rewriting a url which contains a directory which actually exists...
File structure
>support
>sys
>issue
issue.php
.htaccess
(I.e. the directory issue and the .htaccess file are in the same directory: sys)
Rewrite Issues
Then:
RewriteEngine ON
RewriteRule ^issue/(.*)/*$ issue/issue.php?id=$1 [L]
# Note the added /* before $. In case people try to access your url with a trailing slash
Will not work. This is because (Note: -> = redirects to):
http://www.mysite.com/support/sys/issue/1234
-> http://www.mysite.com/support/sys/issue/issue.php?id=1234
-> http://www.mysite.com/support/sys/issue/issue.php?id=issue.php
Example/Test
Try it with var_dump($_GET) and the following URLs:
http://mysite.com/support/sys/issue/1234
http://mysite.com/support/sys/issue/issue.php
Output will always be:
array(1) { ["id"]=> string(9) "issue.php" }
Solution
You have three main options:
Add a condition that real files aren't redirected
Only rewrite numbers e.g. rewrite issue/123 but not issue/abc
Do both
Method 1
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^issue/(.*)/*$ issue/issue.php?id=$1 [L]
Method 2
RewriteEngine ON
RewriteRule ^issue/(\d*)/*$ issue/issue.php?id=$1 [L]
Method 3
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^issue/(\d*)/*$ issue/issue.php?id=$1 [L]
Retrieving the ID
This is the simple part...
$issueid = $_GET['id'];
In your .htaccess you should add:
RewriteEngine On
RewriteRule ^id/([^/]*)$ /get.php/?id=$1 [L]
Also like previous posters mentioned, make sure you have your mod_rewrite activated.
You have to use a file called .htaccess, do a search on Google and you'll find a lot of examples how to accomplish that.
You will need mod_rewrite (or the equivalent on your platform) to rewrite /get.php/id/123 to /get.php?id=123.
I tried and tried the .htaccess method but to no avail. So I attempted a PHP solution and came up with this.
issue.php
<?php
if (strpos($_SERVER['REQUEST_URI'], 'issue.php') !== FALSE){
$url = split('issue.php/', $_SERVER['REQUEST_URI']);
}elseif (strpos($_SERVER['REQUEST_URI'], 'issue') !== FALSE){
$url = split('issue/', $_SERVER['REQUEST_URI']);
}else{
exit("URI REQUESET ERROR");
}
$id = $url[1];
if(preg_match('/[^0-9]/i', $id)) {
exit("Invalid ID");
}
?>
What you're looking for is the PATH_INFO $_SERVER variable.
From http://php.net/manual/en/reserved.variables.server.php:
'PATH_INFO'
Contains any client-provided pathname information trailing the actual
script filename but preceding the query string, if available. For
instance, if the current script was accessed via the URL
http://www.example.com/php/path_info.php/some/stuff?foo=bar, then
$_SERVER['PATH_INFO'] would contain /some/stuff.
explode() it and work on its parts.
EDIT: Use rewrite rules to map the users' request URLs to your internal structure and/or hide the script name. But not to convert the PATH_INFO to a GET query, that's totally unnecessary! Just do a explode('/',$_SERVER['PATH_INFO']) and you're there!
Also, seeing your own answer, you don't need any preg_mathes. If your database only contains numeric ids, giving it a non-numeric one will simply be rejected. If for some reason you still need to check if a string var has a numeric value, consider is_numeric().
Keep it simple. Don't reinvent the wheel!
Just wondering why no answer has mentioned you about use of RewriteBase
As per Apache manual:
The RewriteBase directive specifies the URL prefix to be used for
per-directory (htaccess) RewriteRule directives that substitute a
relative path.
Using RewriteBase in your /support/sys/issue/.htaccess, code will be simply:
RewriteEngine On
RewriteBase /support/sys/issue/
RewriteRule ^([0-9+)/?$ issue.php?id=$1 [L,QSA]
Then insde your issue.php you can do:
$id = $_GET['id'];
to retrieve your id from URL.
I have the url:
http://www.mywebsite.com/i/1Q2GmFuFdZ.jpg
I want to rewrite a rule that makes it go to the file image.php?id=1Q2GmFuFdZ.jpg
In my PHP file, I have this
<?PHP
print_r($_GET);
?>
This is what I have in my htaccess
RewriteEngine On
RewriteRule ([a-zA-Z0-9]+[\.]+[a-z]{3})$ image.php?id=$1
This is the output that I am getting:
Array ( [id] => image.php )
I am not sure what I am doing wrong. I'm no regex pro but I know the basics.
Thanks
EDIT:
Forgot to add 0-9 in expression
mod_rewrite rules are applied recursively. Here is what happens when you request 1Q2GmFuFdZ.jpg:
1Q2GmFuFdZ.jpg matches the pattern, becomes image.php?id=1Q2GmFuFdZ.jpg
Since filename has changed, another iteration is performed with the re-written URL
image.php matches the pattern as well and rewritten as image.php?id=image.php
File name or directory did not change so no further iteration is performed
End result: image.php?id=image.php
Solution: add this rule:
RewriteEngine On
RewriteRule image\.php$ - [L]
RewriteRule ([a-zA-Z0-9]+\.[a-z]{3})$ image.php?id=$1
The second rule acts as a hand-brake. You need to tweak rule #3 as well.
Your Rewrite rule is wrong. It should be like this:
RewriteRule ^i/(.+)$ /image.php?id=$1 [L,NC,QSA]