Hi,
momently I turn my Website to an multilanguage Website.
Problem:
I would like to Read the locale of the user from an GET variable. So the htaccess must write this locale at the end.
If this question already exists please send me the link, or is there an simpler way for the local tell it me.
You could do that using ModRewerite
RewriteCond %{QUERY_STRING} !locale=
RewriteRule ^(.+)$ $1?locale=DE [QSA,NC,L]
Related
I know there's a million similar questions on stuff like this, but clearly there's much I don't understand because I haven't been able to derive answers or a solution to my (as I understand it) fairly simple question.
Basically, I'm trying to get an old site back up, but want a more professional look to it this time round, which includes cleaning up the URLs. A typical page is as follows (hosted locally at the moment, but will be assigned a domain in next few days):
192.168.0.200/album-reviews.php?albid=22
Using the following code, I have been able to achieve the above example page loading via manually typing 192.168.0.200/album-reviews/22 into the browser:
RewriteRule ^album-reviews/([0-9a-zA-Z]+) album-reviews.php?albid=$1 [NC,L]
However, what I want as well is for when the link is clicked on my site, it directs the user to /album-reviews/22 instead of album-reviews.php?albid=22. The only way to get the clean URL at the moment is to manually type it into the bar, links from my site do not get the clean URL, the code I have been playing around with (and have been unable to get working) based on sources I've found is this:
RewriteCond %{THE_REQUEST} /album-reviews/?(?:\.php)?\?albid=([0-9a-zA-Z]+)
RewriteRule ^ /album-reviews/%1? [L,R]
So if anyone could shed some light on how I get all this working as desired, I'd be grateful, I hope my question has been articulated appropriately.
On a side note, If i wanted to include the post title in the URL too like this:
192.168.0.200/album-reviews.php?albid=22&ptitle=my first post
how would alter any code to make it like this:
192.168.0.200/album-reviews/22/my first post
Thank you.
You can use:
Options -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} /album-reviews/?(?:\.php)?\?albid=([^\s&]+)&ptitle=([^\s&]+)
RewriteRule ^ album-reviews/%1/%2? [L,R=301]
RewriteCond %{THE_REQUEST} /album-reviews/?(?:\.php)?\?albid=([^\s&]+)
RewriteRule ^ album-reviews/%1? [L,R=301]
RewriteRule ^album-reviews/([^/]+)/?$ album-reviews.php?albid=$1 [NC,L]
RewriteRule ^album-reviews/([^/]+)/([^/]+)/?$ album-reviews.php?albid=$1&ptitle=$2 [NC,L]
If you want to show the contents of http://yoursite.com/album-reviews.php?albid=22 at the url http://yoursite.com/album-reviews/22, you need these codes:
Your htaccess needs this lines:
RewriteEngine On
Options -Indexes
RewriteRule ^album-reviews/(.*)$ album-reviews.php?pretty=$1 [L,QSA]
And your PHP these:
$pretty = $_GET['pretty'];
$parameters = explode('/',$pretty);
$albid = $parameters[0];
Your user won't be redirected, your website will show directly the page at the pretty url.
Now, what happened? That you instructed htaccess to send everything after album-reviews as a GET parameter called "pretty". Then in your PHP you cut it for every / that appeared in it, and that way you formed the array $parameters. So you can even get more parameters, for every /, they are all in the array $parameters:
$second_parameter = $parameters[1];
$third_parameter = $parameters[2];
$fourth_parameter = $parameters[3];
I need to set username for my website..
like i have a profile id of user say localhost/ajax/profile.php?id=27
I need to change it like localhost/ajax/xyz
How to achieve it? Please give me valuable link or show me the right way to go or help me out.
i am totally newbie in this town so please help me from 0.
i mean can i have to make php file before applying .htcaccess ? if yes then what to write on that php file?
suppose a person choose a username "abc" so how can the script identify that localhost/ajax/profile.php?id=111 is "abc" and we need to rewrite localhost/ajax/abc
In summary : how to make username like Facebook.
Use .htaccess to hide your file name.php extension
RewriteOptions inherit
RewriteEngine On # enables url rewriting
RewriteCond %{REQUEST_FILENAME} !-d # if requested uri is not directory (!-d)
RewriteCond %{REQUEST_FILENAME}\.php -f # and if there is a file named URI+'.php' (-f)
RewriteRule ^(.*)$ $1.php # then if there is any thing in uri then rewrite it as uri+'.php'
You need to use .htaccess to re-structure your links. You need to create.htaccess if its already not there inside your main folder. You can try this out:
RewriteRule localhost/ajax/(.*) localhost/ajax/profile.php?id=$1 [PT,L,B]
Remember that on RewriteRule, you can't use any php scripts. So, You can't rewrite localhost/ajax/profile.php?id=27 to localhost/ajax/xyz readily. You need to pass localhost/ajax/profile.php?id=xyz and above rule will work fine.
Learn more about .htaccess RewriteRule from here.
u can do it with .htaccess url rewrite method.
it will convert ur url localhost/ajax/profile.php?id=111 to localhost/ajax/111
after then u can also modify to localhost/ajax/myusername
try given link and enjoy 100% satisfied answer
http://www.9lessons.info/2009/11/pretty-urls-with-htaccess-file.html
here u will find some extra tools for ur social network
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 got a Q regarding mod_rewrite with php.
currently my userprofile php page has the following link to determine a user:
domain.com?username=john
i would need to convert this url into like this:
domain.com/john.
how can i do this in mod_rewrite??
Something like:
RewriteEngine On
RewriteRule ^domain.com/(.*)$ domain.com/profile.php?username=$1
I found the solution. The initial problem was my profile php page has the following link: domain.com/profile.php?username=john
instead of this, i would need to make it easy to read url like this: domain.com/john.
what i did is:
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ /profile.php?username=$1 [L]
I'm not sure if this has any security breach but do leave your feedback
So basically I want users to be able to go to my website with a URL of something like /45678, instead of having to use /?p=45678, so really I just want to remove the variable name. I've tried using mod_rewrite, but it seems that is only for removing the name when the page is visited.
Here is the current code:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=([0-9]+=$
RewriteRule ^/$ /%1 [R]
Simply change all of your links to /45678 rather than ?p=45678. Or did I misunderstand you completely? Because what I got from your post is that it works properly, unless you manually access the ?p=45678 where as it stays as ?p=45678.
EDIT:
This is what I am using for http://www.madphp.org/dev/, give it a go, works like a charm for me (it also removes the index.php part). To access your now cleaner URL you would simply explode the $_SERVER['PATH_INFO'] variable to get all of the required parameters within your PHP script.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Have you set up mod_rewrite correctly? If so, you can use variables like simple $_GET variables, in this case you must access $_GET['p'] in PHP.
I did this without using .htaccess, but it does query a database. I wrote this a while ago so it uses PEAR DB, adjust to your database/connection method. I'll just copy my code and let you figure out what changes you need.
$db=connect_db();
$name=substr($_SERVER['PHP_SELF'], 20);
$name=strtolower($name);
$id=$db->getone("select id from user where login='{$name}'");
header("Location: /dragonart/profile?user=" . $id);
If you store your information in a database this may be a nice alternative. The downside is that the the URL is not rewritten and the user is ultimately sent to a page with ending in a $_GET variable.
edit:
Just realized that using my method a simpler method can be used for the answer. Since my solution was used to find the id of a user using their username and then send someone to their profile (which requires the id) a better solution would be something like:
$var=substr($_SERVER['PHP_SELF'], $length);
header("Location: /path/to/page?p=".$var);
where $length is the usual length of the URL without the variable at the end.