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]
Related
One pages of site have such adds: domain.com/posts/name.php
Other pages of site have such adds: domain.com/pages/name.php
I need to cut from these adds posts/ and pages/ only.
Firstly, I tried to use in .htaccess next rules:
RewriteRule ^([A-z0-9-]+)$ /posts/$1
RewriteRule ^([A-z0-9-]+)$ /pages/$1
Don't help.
Secondly, I tried to use in .htaccess such rules and conditions:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ posts/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ pages/$1 [L]
As result: image of mistake 404
Also I tried to use such method:
RewriteCond %{REQUEST_URI} ^([A-z0-9-]+)$/(posts|pages)/([A-z0-9-]+)/$1 [NC]
RewriteRule .* domain.com/%1%3
Don't help....
what i am understanding you need to remove the post and pages from the url following code will help you for that.
You would need the rewrite module of Apache: mod_rewrite.
Then do something like this:
RewriteEngine on
RewriteRule ^post/(.*)$ $1
RewriteRule ^pages/(.*)$ $1
Here is the official documentation of mod_rewrite: click
please test it onece I dint test the code but did same for replacing url and its work for me.
Thanks.
The solution of this problem such:
We enter all information about the page in the MYSQL database:
image of database
We generate a page from the database:
generation of page (php)
Set the generated page for Human-Friendly Url:
file .htaccess
So everything works, and POSTS there are in their folder in the database, and PAGES in their folder.
folder in database
All is clean. If you can do better somewhere, then correct me, please.
i want to redirect users to my site based on the url they typed into the address bar but i am problems as what to actually do. forgive me if i am not using the right term, i am a newbie.
this an example of what i want to achieve
www.mysite.com (is my site)
www.mysite.com/pizza (is what the user typed)
i want the browser to take the user to www.mysite.com if he types www.mysite.com and redirect the user to www.mysite.com/base/tweek.php?ref=pizza if he types www.mysite.com/pizza.
how do i do this?
Try adding this to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /base/tweek.php?ref=$1 [L]
Also make sure you have mod_rewrite loaded.
You must create the .htaccess file in the root directory of the page, and write:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /base/tweek.php?ref=$1 [L,QSA]
I think (handwritten, not checked :) ).
Hope it helps, if You have any problems, try to read some htaccess tutorial.
Just use header at the top from your index.php:
header('Location: anotherindex.php');
Let say I have a site
www.mysite.com
Is there anyway to have to have user page urls be
www.mysite.com/username
instead of the usual
www.mysite.com/user.php?id=whatever
I would like to know if this is possible in php without having multiple folders and index pages. I would like to avoid the folder method because from my understanding you would have an issue with efficiency especially if you have to bounce from one user to another. I would like to do this method because it is a lot easier for someone to say get my info from
www.mysite.com/username
than the other option. Any help (tutorials, sites, etc) would be appreciated. Thanks in advance.
I would like to know if this is possible in php without having multiple folders and index pages.
Yes, its possible. You can do it redirecting all requests to your index.php and process this url (route) manually
sample .htaccess for redirecting all request to your index.php
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?route=$1 [L,QSA]
BUT implemention of
www.mysite.com/username
is not easy thing. You have to know all your system usrls such as /register, /login, /post, ... etc and dont allow to register these usernames.
I recomend you implement this scheme:
www.mysite.com/~username
or
www.mysite.com/user/username
Apply front controller pattern with any kind of router [e.g. klein], then you'll need a single, universal .htaccess file in your webroot:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Yes sure, use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /user.php?id=$1 [L,QSA]
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!
I've been working on this for a while and have tried a lot of different solutions I've seen on the web and can't seem to get this to work.
I have a site at www.mydomainname.com. The page that I want to handle ALL page requests is www.mydomain.com/index.php. I'd also like to set this up to work for any other domains that I point to this code base (using wildcards would be the way to go for that I think).
So the following URL types (or any other) should automatically go to index.php, while still keeping the original URL structure in the browser address bar:
www.mydomain.com/
mydomain.com/
www.mydomain.com/item/111
www.mydomain.com/item/itemname/anothervariable/value
www.mydomain.com/item/itemname/?variable=value
I'm using PHP 5 and a recent version of Apache with mod_rewrite enabled.
Thanks in advance for any help!
Simple:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !\.(jpg|gif|ico|png|bmp|css|js)$
RewriteRule .* index.php
You could use the follow RewriteRule
RewriteEngine On
RewriteRule ^(.*)$ /index.php?originalUrl=$1
Untested, but it should work. You will then also have the original URL available in the 'originalUrl' GET parameter for further parsing.
Include this once per .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteRule (.*) index.php
If you need the information from the matched URL you can modify your RewriteRule to match portions of the old URL or just include everything by using the variables $1 and so forth. If for instance you wanted to get the item number passed in quickly to index.php, you could use this rule:
RewriteRule item/(.*)$ index.php?item=$1