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]
Related
I already use this htaccess rule already and it works
RewriteRule ^community/profile/([^/]+) community/profile/profile.php?id=$1
So, when I'm on a profile page for example website.com/community/profile/example-user, I will be able to extract the user from the id=$1 part.
I need to extend this to two folders, where example-user can have 2 sub-folders, like website.com/community/profile/example-user/folder1/ and website.com/community/profile/example-user/folder 2/
I tried the following rule:
RewriteRule ^community/profile/([^/]+)/([^/]+) community/profile/profile.php?id=$1&car=$2
Unfortunately though, this breaks the whole rule and it doesn't work. I got the idea from this post https://webmasters.stackexchange.com/questions/65785/rewrite-up-to-three-folders-into-three-query-string-parameters-to-a-php-script
Also, I tried
RewriteRule ^community/profile/../([^/]+) community/profile/profile?id=$1&car=$2
Also didn't work.
To make things clear, if one only goes to the 'first' folder, in this example it is /example-user/, the car variable should just be empty. How can I write this rule so that it will put that second 'folder' into a second variable so I can extract it later in my php file?
Make sure to use anchor $ for precise matching in regex
Include dot in your character class to avoid matching it again
Rules :
RewriteRule ^community/profile/([^/.]+)/?$ community/profile/profile.php?id=$1 [L,QSA,NC]
RewriteRule ^community/profile/([^/.]+)/([^/.]+)/?$ community/profile/profile.php?id=$1&car=$2 [L,QSA,NC]
I already create a .htaccess in my folder and would like to make the URL:
www.example/good/page
toward:
www.example/page?type=good
my current document is written as:
RewriteRule ^(\w+)$\/page page.php?type=$1 [NC,L]
but it didn't work, and I don't know how to check it is correct or not
would anyone able to provide some example for that?
thanks!
Make sure your rewite engine is on, and it is rewriting the base. Change your code like the following:
RewriteEngine on
RewriteBase /
RewriteRule ^/?([^/]+)/page?$ "page.php?type=$1" [L,QSA]
And then your rewrite rule must look like this.
The RewriteRule basically means that if the request is done that matches ^/?([^/]+)/page?$ (matches any URL except the server root), it will be rewritten as page.php?type=$1 which means a request for page.php be rewritten as page.php?type=good).
QSA means that if there's a query string passed with the original URL, it will be appended to the rewrite (example.com/good/page?id=2 will be rewritten as page.php?type=good&id=2.
L means if the rule matches, don't process any more RewriteRules below this one.
Try this and let me know. Accept the answer if it worked for you.
So I'm currently using my .htaccess to write my URL to be more URL friendly.
But I'm having trouble getting the fourth variable that could be more folders. I'll try and explain it as best I can below.
So at the moment I have the following URL working:
www.mysite.co.uk/first/second/third
Now for this I am using the rewrite rule:
RewriteRule ^e/([a-zA-Z0-9-/]*)/([a-zA-Z0-9-/]*)/([a-zA-Z0-9\-\/]*)$ index.php?var_1=$1&var_2=$2&var_3=$3 [L]
My problem comes when I one of the following happens:
www.mysite.co.uk/first/second/third/fourth
www.mysite.co.uk/first/second/third/fourth/fifth
www.mysite.co.uk/first/second/third/fourth/fifth/sixth
From the above I want the following variables to be returned:
$_GET['var_1'] = first
$_GET['var_2'] = second
$_GET['var_3'] = third/fourth
OR
$_GET['var_3'] = third/fourth/fifth
OR
$_GET['var_3'] = third/fourth/fifth/sixth
So basically I always have a 'var_1' and 'var_2' set but the third variable can be the rest of the URL.
I hope this makes sense and that it's possible.
I'm not sure why you your rule starts with ^e/
if your urls will be in format : www.mysite.co.uk/e/first/second/third/fourth
then use this regex:
RewriteRule ^e/([a-zA-Z0-9-]+)(/([a-zA-Z0-9-]+))?(/([a-zA-Z0-9-/]*))?$ index.php?var_1=$1&var_2=$3&var_3=$5 [L]
If you need it without /e/ at start www.mysite.co.uk/e/first/second/third/fourth
then remove /e/ from start
RewriteRule ^([a-zA-Z0-9-]+)(/([a-zA-Z0-9-]+))?(/([a-zA-Z0-9-/]*))?$ index.php?var_1=$1&var_2=$3&var_3=$5 [L]
Example above will work even with url-s like /first and /first/second but if you don't need it and you want to have this rule just in case of 3 or more params then you can simplify rule:
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-/]+)$ index.php?var_1=$1&var_2=$2&var_3=$3 [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.
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