I am using .htaccess code in order to pass pages titles to the url and then retrieve them with php from mysql tables when the page loads.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
I am using this php code to explode the url and get the title:
$passed_url = $_SERVER['REQUEST_URI'];
$passed_url = explode("/", $passed_url);
$passed_url = end($passed_url);
Now I want to create a user profile page like so www.website.com/users/User_name_in_here
how do I check with php if this is a users page or it is just a regular page which can be writer like so: www.webiste.com/Page_title_in_here
is there a better way to do this ?(I am just a starter in php)
One way is to check $passed_url[1]. If it's 'users' then you know you're on the users page.
A better way would be to use index.php as a front controller and pass the request to a controller based on the request uri. there are a number of ways to do this.
Some frameworks map the uri to a class::method. So your url would be changed to /users/view/username and the users::view would be called.
I prefer to write regular expressions and which ever regular expression is matched controls which controller is loaded.
You can actually change your Rewrite Engine .htaccess file this way:
RewriteEngine On
RewriteRule (.*)-(.*)\.ptf$ ./?page=$1&id=$2&%{QUERY_STRING}
RewriteRule ^([^/]*)\.ptf$ ./?page=$1&%{QUERY_STRING} [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ ./?page=$1&id=$2&%{QUERY_STRING}
So, this rule, what it exactly does is:
when the URL is www.website.com/Page_title_in_here,
it goes to www.website.com/?page=Page_title_in_here
and when you put this way, www.website.com/users-User_name_in_here,
it redirects to www.website.com/?page=users&id=User_name_in_here
So you can manipulate with the $_GET["page"] and $_GET["page"] variables!
What say? I do this way. Hope this helps you!
Related
I'm writing a site in PHP that I would like to have structures, nice, and pretty URLs in. I know in one site I had rules setup in the .htaccess, but I have to have some of these URL rules created dynamically, so is there a way to do that in the PHP itself? I feel like Wordpress does this somehow with its pages.
Basically, if I go to this page:
site.com/users/Xan
it will technically be loading:
site.com/users?name=Xan
I can write the patterns (using regex) fairly easily. I just need to know how to make this possible.
Option 1:
To redirect using .htaccess to /users enter this in:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ users?user=$1 [L,QSA]
You can then get your username as $user by simply doing:
$user = $_GET['user'];
Option 2:
If that's not what you're after and you're looking to redirect /users/username to $_GET['page'] in the index.php, stick this into your .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
So let's explode that response in PHP:
$urlarray = explode("/",$_GET['page']);
Now
echo $urlarray[0]; // users
And...
echo $urlarray[1]; // username
Hi I want To Ask How Can I convert http://abc.tk/comments.php?post_referrel_id=16 to http://abc.tk/comments.php?post_referrel_id/16 using .htaccess mod_write
means the user see this link
abc.tk/comments.php?post_referrel_id/16
when he clicks a hyperlink having source
abc.tk/comments.php?post_referrel_id=16
you need research, sir.
check this easy tuto
And for your tips, don't use ? in the rewritten url.
here is a quick code to do that (with ? in the rewritten url)
RewriteEngine on
RewriteRule ^comment.php?post_referrel_id/([0-9]+)$ comment.php?post_referrel_id=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^comments.php?post_referrel_id/(.*)$ comments.php/post_referrel_id=$1 [L,QSA]
You may have to add a redirect from one form to another. The above code just tells the server that the /16 is the same with =16
use these lines of code :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc.tk
RewriteRule ^comments.php?post_referrel_id=([0-9]+)$ comments.php?post_referrel_id/$1 [R=301,L]
this will be redirect comments.php?post_referrel_id=16 to comments.php?post_referrel_id/16
I am looking for some help in my htaccess to better handle my urls when using a pagination function.
Currently my htaccess looks like this:
RewriteEngine On RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?alias=$1 [QSA,NC,L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?alias=$1/$2 [QSA,NC,L]
basically, I have two rules. At first I only had the second one, to direct all requests to my index page, where I then parse out some details of the url in order to show the correct content. I found that it didnt work for urls with only one sub directory, like example.com/bars, but only two directories like, example.com/bars/bar-name, so I added the rule before it.
This has been working great for me. All of my urls look great and the ?alias=bars/bars-name has been completlely hidden and now looks like /bars/bars-name
Recently though I have integrated a pagination php function. This function creates urls that looks like this:
example.com/bars?alias=bars&info=mysql_table_bars&page=2
Basically, 'info' is the table name that the pagination function queries, and 'page' is the current page. It seems 'alias' also started showing in the url once I started using this function.
What I would like is for my pagination to look like these examples:
example.com/bars/page/2
example.com/hotels/page/5
etc...
What do I need to add to my htaccess to handle this situation? Also, is there a way to simplify my existing two rules?
Regarding your original rules. The conditions that you have only get applied to the immediately following RewriteRule, so you need to duplicate them:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?alias=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?alias=$1/$2 [QSA,NC,L]
For the new pagination stuff, firstly, it is really bad to be pulling database table names from the query string, unless they've been validated somehow (which may be what's happening). But even then, it's still an information disclosure problem. So you can use these rules to always remove them:
# This matches the actual request when there's pagination in the query string
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/\?]+)\?alias=([^&\ ]+)&info=.*&page=([0-9]+)
# an optional sanity check, does the request URI match the "alias" param?
RewriteCond %1:%2:%3 ^(.*):\1:(.*)$
# redirect the browser to the URL with the table name removed
RewriteRule ^ /%1/page/%2? [L,R=301]
Then you need rules to internally rewrite them back to the request with the query string:
# rewrite it back to the query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/page/([0-9]+) /$1?alias=$1&info=mysql_table_$1&page=$2 [L]
Currently I'm appending the following url parameter:
www.somesite.com/?page_type=view
My php script uses this to determine which page view to load:
if (isset($_GET['page_type'])) {
$page_type = $_GET['page_type'];
$this->pageType($page_type);
} else {
$this->home();
}
I would like to be able use the following url to achieve the same thing:
www.somesite.com/view
So I need to redirect all requests to index.php while maintaining the original url input.
Then I can just use
$_SERVER['REQUEST_URI']
to get at the name of the page view.
Looking for some htaccess advice,
Thanks in advance!
You can use something along the lines of:
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>
This will redirect all requests that don't resolve to actual files/directories to your index.php. The ?url=$1 will contain the request but this may be optional in your case as you can still just get it from $_SERVER['REQUEST_URI'].
I left a RewriteBase line in there (commented out) as uncommenting can help with come server setups.
Another option you have is to simply set up your htaccess file to redirect any 404 (unresolvable) requests into the index file with a flag set like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]
From here you can use PHP's access to the $_SERVER['REQUEST_URI'] information to parse things out in PHP and manually push variables into the $_REQUEST superglobal as needed to "map" your search engine friendly links into the application.
This gives you a lot more scalability than simply forcing the /pagetypevar format on all urls.
What happens later when you want to nest pages into subcategories? /info/about_us, /info/contact_us
What happens if you want to store other variables like items per page or pagination in the url? /products/1_My_supercool_item, /products/2_Another_item
Using a system that redirects all unresolved requests into the application framework, and allowing the application framework to do the remapping of urls will give you the most control and the most scalability.
Use a rewrite rule like this:
RewriteEngine On
RewriteRule /(home|view) index.php?page_type=$1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /^(.*)$ /index.php?page_type=$1 [L]
I am looking for your recommandations on what would be the best way to implement friendly URLs.
What I currently do is redirect all 404 requests to folders or files that do not exist to index.php.
index.php reads the query string and makes a database call to see if the url is in the page_urls table then based on the page type fetches content etc etc.
The .htaccess contains the following lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
Is there a more "clever" way of doing this please? Thank you.
Thank you.
The best way I've found is to do something like the following:
RewriteEngine on
RewriteRule ([a-zA-Z0-9_-]*)\.html index.php?page=$1 [L]