php mod rewrite - php

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

Related

how to redirect url to specific url

I am trying to redirect my blog post to specific URL https://example.com/detailblog.php?my-title&s=9 to https://example.com/my-title/9
RewriteEngine on
RewriteRule ^/blog/([0-9]+)\.php /blog/detailblog/.php?s=$1
RewriteEngine on
RewriteRule ^/blog/([0-9]+)\.php /blog/detailblog/.php?s=$1
it's not working.
I would recommend reading beginner guides for URL rewriting like: https://aloneonahill.com/blog/url-rewriting-for-beginners/ and using test tools like: https://htaccess.madewithlove.be/.
It seems to me you have not understood the syntax, based on the example you provided.

PHP GET data issue

I have an issue in posting php data.
I have small website and I made a php file cat.php.
Normally to post id it should be like that cat.php?id=.
I'm asking if I can do it like this cat-ID.php.
I think the following should work for your scenario:
RewriteEngine on
RewriteRule cat-([0-9]+)\.php cat.php?id=$1 [L]
This would rewrite /cat-3.php to cat.php?id=3 and /cat-666.php to cat.php?id=666 and so on.
Your first example is POST, not GET (e.g. cat.php?id=5 would be $_GET['id']). You can, though, use htaccess rewrites to create URLs something like cat-[id].html instead, which is cleaner. See examples: http://httpd.apache.org/docs/trunk/rewrite/remapping.html
Update:
Mod Rewrite example. This would be in .htaccess:
RewriteEngine On
RewriteRule cat-([0-9]+)\.html cat.php?id=$1 [L]
Then in PHP, you could:
<?php
echo $_GET['id'];
?>
And you should be able to hit /cat-5.html and see 5 echoed.

Custom URL with PHP

I have a small question to ask. Is it possible, via php or htaccess, to change a url like: miodominio.com/users.php?idu=x into something like miodominio.com/username ?
I want it to be Facebook style...Where "username" is the username chosen by idu = x.
There are multiple ways to solve this problem, but here's one that always suits my needs.
Guide all your URL requests through the index.php first and resolve the request in your PHP code second.
1) Use an .htaccess file to direct all URL's through index.php. You'll find one way here by the CodeIgniter framework and a more advanced explanation here. I recommend the CodeIgniter .htaccess guide first if you're inexperienced with .htaccess.
2) Second, use the $_SERVER variable in PHP to extract the URL. Probably with the help of the $_SERVER['REQUEST_URI'], you'll find '/username/' which you can then use to extract the user's data and serve it to them.
Good luck and beware of URL injections using this method.
You need to use apache's mod_rewrite for this. It can translate miodominio.com/username to miodominio.com/users.php?idu=x. There are some good guides about this which are easy to find with Google.
You can try to use this mod_rewrite pattern (add it to the .htaccess):
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?idu=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ users.php?idu=$1
you have to write a clean URL in your .htaccess file like :
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$ users.php?idu=$1
Put the following in your .htaccess
RewriteEngine on
RewriteRule ^([a-z0-9_-]+)/?$ /users.php?idu=$1 [NC]
The [NC] will make it case-insensitive, if you accept only lowercase username, remove the [NC] from the last.

remove http from URL in .htaccess (mod_rewrite)

Been stuck on this for ages and tried loads of fixes but just can't get my head around it!
I run a site where the content of the pages are generated based upon a URL. For example:
http://www.mysite.com/http://www.supercheese.com
Would generate a mashup of content from mysite.com and supercheese.com
The .htaccess i use for this (at mysite.com) is:
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteRule assets/.* - [L]
RewriteRule ^(.*)$ test.php?url=$1 [NC,L]
</ifModule>
So basically the second URL is passed in a php string.
My question is this, I need to remove the http:// from the address using .htaccess
E.G. If someone types:
http://www.mysite.com/http://www.supercheese.com
I need it to become:
http://www.mysite.com/www.supercheese.com
Many thanks in advance for taking a look at this. You guys rock.
You can simply use a RedirectMatch
http://httpd.apache.org/docs/2.0/mod/mod_alias.html#redirectmatch
Try with :
RedirectMatch ^/https?://(.*)$ http://www.mysite.com/$1
Edit : you have to put this before rewrite rules
Edit : add / before http
Edit : David is right, take a look a his answer and change your way of writing these urls
It looks to me like the url scheme is inherently problematic.
Using an unencoded colon : in the url - other than following the http to specify the access protocol - seems to make the server think that it is doing authentication of the form http://username:password#hostname/.
I know it doesn't directly answer the question, the solution is to change the url-scheme. ;-(

how can I rewrite URL to fancy url?

when my user logs in, I want the get variables that were sent rewrote onto the URL like so:
http://mysite.com/mygetvar1/mygetvar_value1/mygetvar2/mygetvar_value2/
or
mysite.com/mygetvar1=mygetvar_value1/mygetvar2=mygetvar_value2/
How can I do this?
Please help! Thanks!
Codeigniter can offer you like that. Many other PHP frameworks offer that as well.
Try this.
RewriteRule /([^/]*)/([^/]*)/([^/]*)/([^/]*)/ /login.php?$1=$2&$3=$4 [R=301]
RewriteRule /([^/]*)=([^/]*)/([^/]*)=([^/]*)/ /login.php?$1=$2&$3=$4 [R=301]
First you need the mod_rewrite module enable.
After, put this in your config file :
RewriteEngine on
RewriteRule ^ads/(rims|tires|combo)/([0-9]+)/(.+).html /ad.php?noAds=$2 [L,QSA]
This is a example.
Your url will look like : http://www.yourwebsite.com/ads/rims/331/title.html
but you will call the url : http//www.yourwebsite.com/ad.php?noAds=331
For your regex , you should use a site like http://www.rubular.com
You can use the .htaccess file or put in directly in the httpd.conf file

Categories