URL Rewrite: PHP variable as directory - php

Alright, how would I get something like:
domain.com/friends.php?id=Fred
Into something like:
domain.com/Fred/friends.php
Previously, I have used:
RewriteRule ^([A-Za-z0-9]+)$ profile.php?id=$1 [L]
To turn
domain.com/profile.php?id=Fred
Into
domain.com/Fred
I know there is a way to do it, I just don't have the knowledge of the operators that would put that extra 'friends.php' in the RewriteRule. Thanks for the help, if my explanation made sense.

You're close:
RewriteRule ^([A-Za-z0-9]+)/friends\.php$ friends.php?id=$1 [L]

Related

.htaccess filename to url

I've been searching and cannot find an answer that suits my needs. It is a rather simple question I assume.
The point is that I need to rewrite my files to a name of my liking. For example I have, 'www.mydomain.com/account_index.php' which I want to rewrite to www.mydomain.com/account
Thus far I had this, but that doesn't seem to work with multiple lines.
Rewriteengine on
rewriterule ^support account_ticket.php [L]
rewriterule ^account account_index.php [L]
So in short, I want my website to redirect /account to account_index.php and the other way around.
Thanks in advance!
I found the answer for those that are wondering.
I had to put a RewriteCond before every RewriteRule.
So if I wanted to go to www.mydomain.com/account. I'd have this:
RewriteCond %{REQUEST_URI} ^/account$ [NC]
RewriteRule ^account account_index.php [NC,L]
This means that /account is now linked to account_index.php.
Have a nice day!

htaccess dynamic query string

maybe this is a stupid question
i have current code below:
RewriteEngine On
RewriteRule ^(.*) /image/modify.php?pic=$1 [QSA]
and i enter the url http://site.com/image/20121207/test.jpg&w=256
will display the resized image.the original url is:
http://site.com/image/modify.php?pic=20121207/test.jpg&w=256
but how can i change &w= to ?w= ?
the w= are random,perhaps someone will enter h= / r= ...etc
please give me some hint
The Apache rewriter is intelligent enough to process query parameters correctly. That's what the QSA is for, "query string append". You can access 20121207/test.jpg?w=256 and it should be rewritten to modify.php?pic=20121207/test.jpg&w=256 correctly.
Try this:
RewriteRule ^([^/]+)/([^/]+)/([^?&]+)?(.*) /image/modify.php?pic=$1$2$3&$4 [QSA]
So easy :
RewriteEngine On
RewriteRule ^([^/]*)(?)(.*)$ /image/modify.php?pic=$1&$2 [QSA]
Hope it work ....
How about this:
RewriteEngine On
RewriteRule ^image/([^/]*)(?)(.*)$ /image/modify.php?pic=$1&$2 [QSA]
and enter the url:
http://site.com/image/20121207/test.jpg?w=256
i has try it, i think it will work ...
Good luck

php how to replace ?page=1 to 1

I'm just wondering how other websites replace there get variable.
example:
http://www.example.com/page.php?page=1
replaced:
http://www.example.com/page/1
indeed, just what ssx said.
You need a .htaccess file in the root of the website, with some content that looks like this (i use this one)
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([\(\)\|a-zA-Z0-9_-]+)/([0-9]+)/?$ /index.php?ext=$1&cat=$2&name=$3&urlID=$4 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([\(\)\|a-zA-Z0-9_-]+)/?$ /index.php?ext=$1&cat=$2&name=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/p=([0-9]+)/?$ /index.php?ext=$1&cat=$2&p=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ /index.php?ext=$1&cat=$2 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /index.php?ext=$1 [L]
I'd say take a look at an answer I provided someone else a while back. I'd type it all out here again or copy and paste it but the link is much easier.
PHP dynamic DB page rewrite URL

Rewriting URL rule, example like StackOverflow

What would be the apache rewrite url to extract the id number (434376) from this url?
http://stackoverflow.com/questions/434376/unique-url-slug
something like
RewriteEngine On
RewriteRule ^questions/([0-9]+)/?$([A-Za-z0-9-]+) post.php?post_id=$1 [NC,L]
...but it doesnt work on my site...any tips?
thanks
Tested:
RewriteRule ^questions/([0-9]+)/?.*$ rewrite.php?post_id=$1 [NC,L]
Well if the / is optional it probably should be:
RewriteRule ^questions/([0-9]+)/?([A-Za-z0-9-]+)?/?$ post.php?post_id=$1 [NC,L]
Let me know if it works..

help with htaccess

I want to redirect this:
foo.com/xxx
... to this:
foo.com/somepage.php?id=xxx
This is how I do it:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)$ http://foo.com/somepage.php?id=$1 [L]
the problem now, is that foo.com doesn't work any more.
I can't see foo.com neither foo.com/index.php
help me! :)
RewriteRule ^/([^/]+)$ /somepage.php?id=$1 [L]
Be careful tho, foo.com/1 is a BAD move on SEO.
1) You don't need the RewriteCond line. It's intended to determine WHEN your rule applies, not WHICH PART of the request.
[EDIT] UNLESS, your RewriteCond is there to make this rule apply whenever the query string is empty. If that's it, then there's nothing wrong with it.
2) I think you need to include the first / in your rule, like this:
RewriteRule ^/([^/]+)$ /somepage.php?id=$1 [L]
[EDIT] Both your version and mine will match your index.php file, which might explain why your site is broken right now. If you don't want to match php files, you could add another condition like
RewriteCond ${REQUEST_URI} !^.*\.php$
3) For a rule like this, it might be helpful to add /? at the end (outside the parens, before the $), in case they look for foot.com/xxx/, which makes sense if you want it to look like a directory.
Try:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(xxx=)$ /somepage.php?id=$1 [L]
Of course you didn't clearly specify what xxx= is...

Categories