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.
Related
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]
Im struggling with a .htaccess settings to get following:
To create Multi-language functionality in my CMS without duplicating the productdatabase I would like to get the content of the default language, but keeping the right url in the addressbar.
For example:
http://www.myshop.com/en/webshop get the content of http://www.myshop.com/webshop
http://www.myshop.com/en/collection get the content of http://www.myshop.com/collectie
And also for the products:
http://www.myshop.com/en/webshop/item1 get content of http://www.myshop.com/webshop/item1
http://www.myshop.com/en/collection/product1 get the content of http://www.myshop.com/collectie/product1
In short:
In case of calling the folders '/en/webshop/' and '/en/collection/' there must be a rewrite to '/webshop' and '/collectie'.
Anyone got a clue?
Thanks in advance...
What about:
RewriteEngine on
RewriteRule ^/?en/webshop/(.*)$ /webshop/$1
RewriteRule ^/?en/collection/(.*)$ /collectie/$1
The first line just enables mod_rewrite
The second line (1st rule) matches anything under /en/webshop and discards the /en
The third line is similar to the second but it also "renames" collection to collectie
If you have more folders/paths you want to redirect I 'd suggest adding the rule:
RewriteRule ^/?en/(.*)$ /$1
which is rewriting anything that starts with /en.
Hope it helps, note it is not tested
Use below rule:-
RewriteEngine on
RewriteRule ^en/(.*)$ /$1 [L,R=301,QSA]
OR
RewriteEngine On
RewriteRule ^(.*)/en/(.*)$ $1/$2 [R=301,L]
Hope it will help you :)
I am trying to rewrite links on my site.
Possible links before rewrite and what I want to see after rewrite:
/index.pl?mode=users&action=add - /users/add/
/index.pl?mode=streets&action=edit&id=7 - /streets/edit/7
/index.pl?mode=users&action=info&id=7 - /users/info/7
etc
.htaccess content:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
RewriteRule ^([A-Za-z0-9-]+)/((edit|delete|info))/([0-9]+)$ index.pl?mode=$1&action=$2&id=$3 [L]
RewriteRule ^([A-Za-z0-9-]+)/((add))/?$ index.pl?mode=$1&action=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.pl?mode=$1 [L]
/users/add/ or /street/add/ are working properly, but...
Problems:
/users/edit/xx - I can't accept ID in perl script. Why?
/users/info/xx - I can't even get to info section /?mode=users&action=info&id=7 page (it have to show blank table with wrong ID
Btw... My site has 'switch' structure. I mean if mode is users, then it load "users.pl", if mode=streets loading "streets.pl", etc. About 2nd problem - sure I have info section at users.pl! And link /?mode=users&action=info&id=7 work perfect.
p.s.: added php-tag because it is not 'perl' problem , but php is equal to perl, so php-followers can help me too
p.p.s.: sry for my not very good english.
You have too many parentheses, which makes your backreferences off-by-one:
RewriteRule ^([A-Za-z0-9-]+)/(edit|delete|info)/([0-9]+)$ index.pl?mode=$1&action=$2&id=$3 [L]
RewriteRule ^([A-Za-z0-9-]+)/(add)/?$ index.pl?mode=$1&action=$2 [L]
Those two rules had too many parentheses around the ((edit|delete|info)) and ((add)) (though the "add" isn't affected because $2 correctly backreferences it).
I'am redirecting about 100 hmtl pages to a single PHP page (example.php) using .htaccess. It is working perfectly.
I've pagination on that page (example.php) but I am using the original HTML page URL (example.html?page=2&limit=20)
so example.html, example1.html, example2.html, example3.html are all redirecting to example.php.
The address bar is still showing ".html" URL but due to .htaccess redirection the example.php is rendering.
when is click on a pagination link (example.html?page=2&limit=20) the browser address bar shows correct .html URL and query string.
I've tried to get the values of page, and limit using $_GET and $_REQUEST in (example.php) but i am not successful.
Please help me in reading the (example.html?page=2&limit=20) query string parameeters .
Edit Code ported from comments:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^page-(.*)$
RewriteRule ^page-(.*)$ size-content.php?sef=$1 [L]
Add the QSA flag, which means "query-string append" to be sure the existing query string is ported into the rewritten URL.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^page-(.*)$
RewriteRule ^page-(.*)$ size-content.php?sef=$1 [L,QSA]
.htaccess modify your server configuration.
if you are making redirection then you change your request.
Try mod_rewrite if you are using Apache of course.
RewriteEngine On
RewriteRule ^example\.html\?(.*) example.php?$1
Mod rewrite is module to Apache. It is not allowed on most free hostings.
Yasir - you can resolve this problem by two ways:
1) Re-write rules for .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^page-(.).html(.)/(.)$
RewriteRule ^page-(.).html(.)/(.)$ size-content.php?sef=$1&page=$2&limit=$3 [L]
This rule will handle: page-example.html?page=2&limit=20
I hope - you will easily understand the above rule.
Note: Keep one thing in your mind that every link should be in same pattern if you change rule in htaccess.
2) You can resolve this problem on your "size-content.php"
Suppose page-example.html?page=2&limit=20
$_GET['sef'] = example.html?page=2&limit=20 [according to you .htaccess]
Now you can parse this string via explode function
Thanks
I've been searching all over the web and haven't yet found any solution to this issue. I'm hoping you could shed some light on the situation.
I have my index file set up like this:
<header></header>
<div id="main">
<?php
if(isset($_GET["p"])) $p = $_GET["p"];
else $p = "home";
if(file_exists("pages/{$p}.php")) include("pages/{$p}.php");
?>
</div>
which makes me load my pages with a ?p=contact href.
Say I would like to display a users profile. I'd then create a subfolder in my "pages" folder, making the relative path pages/users/profile.php, thus the href ?p=users/profile&uid=5. But that leaves an ugly URL (as well as SEO rating).
How would I rewrite that URL to look like /users/profile/5?
EDIT:
I've tried the following, resulting in HTTP 500:
RewriteRule ^([^/]*)/([^/]*)$ /?p=$1&uid=$2 [L]
EDIT: My .htaccess file, located directly inside root folder: http://pastie.org/2268239
Line 338 is where I want to achieve this (currently just a comment).
Simplest answer for both your situations would be to add this in your .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php?$1 [L]
This will redirect all traffic on your domain to your index.php file.
You could then determine what to do in your script using the uri in $_SERVER["REQUEST_URI"]
I achieved the desired effect by adding these three lines:
RewriteRule ^([a-zA-Z]+)$ index.php?p=$1 [L]
RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)$ index.php?p=$1/$2 [L]
RewriteRule ^([a-zA-Z]+)/([0-9]+)$ index.php?p=$1&uid=$2 [L]
This allows me to access /contact, /users/index and /users/profile/5.
I'm not a php guy, but I tried this with my rewrite.
RewriteRule ^/(.+)/([^/]+)$ /index.php?p=$1&uid=$2 [L]
In this case, for the p parameter, you're looking for all chars up to the last slash, so the first part of this takes anything, otherwise it's going to stop at the first slash (users instead of users/profile).
Then it looks for a slash and keep (not-slash). The (.+) will be greedy, so it will go up to the last slash before the end.
Then it occurred to me the last part doesn't need to avoid slashes. Since the first part is greedy, the explicit / slash is going to BE the last slash. So it's even simpler:
RewriteRule ^/(.+)/(.+)$ /index.php?p=$1&uid=$2 [L]
I like the .+ to require something, at least when first figuring these out. If later you know they can be optional, you can do .*, but usually that ends up being a different page or a different rule.
These rules do expect all urls to be in this format, which is what you're asking. But maybe it's a little too grabby, so it could exclude urls that really have a .htm or .php or whatever.
RewriteRule ^/(.+)/([^.]+)$ /index.php?p=$1&uid=$2 [L]
This looks for anything up to the last slash, then anything without a dot in it. If it has a dot, this won't apply. So if it's a "regular" url, this will leave it alone. This might help with the 404 problem, in case the 404 page is getting caught by this.