I wanna turn PHP dynamic URLs into static URLs.
For example, I want URLs like
http://www.example.com/book.php?title=twilight to become http://www.example.com/book/twilight
and http://www.example.com/writer.php?name=meyers to become http://www.example.com/writer/meyers
When it's done, will my form validation on the site change?
My URL rewriting needs might not be much too complicated.
I'm developing it locally using XAMPP, Apache and MySql. Later I'll put it online.
How do I do that?
Is this kind of URL rewriting technique the most adviced for SEO?
You would use mod_rewrite for that. If you do a bit of searching on here for that, you'll likely find lots of other questions about how to create mod_rewrite rules similar to what you want to do.
Yep. U need to use mod_rewrite. Also do a search for .ht_access files. You can put your rewrite directives in .ht_access files and drop them in to whatever directory on your server where you want them to take effect.
For the type of rewrite you want this rule generator should be of use to you:
http://www.generateit.net/mod-rewrite/
And yes the URL you're trying to achieve is seo friendly.
You can use a .htaccess file (or better apache.conf) to forward all requests to index.php with mod_rewrite:
Options +FollowSymLinks
IndexIgnore */*
<ifmodule mod_rewrite.c>
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
</ifmodule>
There you can use $_SERVER['REQUEST_URI'] to interpret the request.
Related
I have recently change my hosting and i need a htaccess rewrite rule for my files. I tried many examples but no one really works for my case. I have never been really good in htaccess and on my older hosting i didn't really need anything it just worked but here is not. Basically i want that my PHP files are without extensions and treated like a directory. So for example i have a URLs like these:
www.domain.com/file1/{id}/{nick}
So for example:
www.domain.com/myfile1/104/username
www.domain.com/myotherfile/455/nick
File1 in this case is a PHP file and {id} and {nick} are changable. I have this structure on my entire site for many other PHP files etc. So if possible i want one universal rule for all files. I tried with htaccess to remove php extenstion etc but all I got is 404 error. Also URL in browser should stay friendly without PHP extension. So in my case if i rewrite my URL manually in:
www.domain.com/file1.php/{id}/{nick} it worked but i don't want to change all the links etc on my website. So all i want is to hide PHP extension and treat PHP files as directory.
Thanks
You can use this single and generic front controller rule in site root .htaccess:
AcceptPathInfo On
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/(.*)$ $1.php/$2 [L]
This is my Blog page's url
www.mysite.com/blog/?id=1
www.mysite.com/blog/?id=2
www.mysite.com/blog/?id=3
But I want to look my url look like
www.mysite.com/blog/1
www.mysite.com/blog/2
www.mysite.com/blog/3
I used the following Rewriting in the .htaccess.
But I am getting Error 404 from my System.(Though the subdirectory /blog is there)
What is the mistake done by me and How can i fix this ?
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ blog/?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ blog/?id=$1
Firstly check mod_rewrite is enabled on apache:
Then add the following to your .htaccess file
RewriteEngine On
RewriteRule ^/?blog/([a-zA-Z0-9_-]+)/$ /blog?id=$1 [L]
Also check AllowOverride All is given in the directory for this module in apache.
Following link can be useful:
AllowOverride for .htaccess on local machine giving 403 Forbidden
I spent almost two days last year chasing this one!
Many Linux distroes (at least Debian-based) prevent you from using a .htaccess file to override the Apache configuration files by stating AllowOverride None in the default config file.
You can change the line to AllowOverride All by editing /etc/apache2/sites-available/000-default
Hope this helps.
This is called Pretty URL's or SEO Friendly URLs, this can be achieved by several ways.
One way is doing everything yourself and modifying the .htaccess to look something like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^users/(\d+)*$ ./profile.php?id=$1
RewriteRule ^threads/(\d+)*$ ./thread.php?id=$1
RewriteRule ^search/(.*)$ ./search.php?query=$1
Note that this requires your webserver to be Apache.
Other ways (which actually work somewhat the same) is by using PHP Frameworks which often have Routing with Pretty URL's implemented so you don't have to reinvent the wheel.
This is Laravel's approach of creating routes and pretty url's:
Route::get('/blog/{id}', function() {
// Do something
});
Take a look at Laravel: http://www.laravel.com. I really love this framework and its features.
Ruby on Rails has this implemented by default as well. But I assume you are not using it since you are programming PHP.
For a good tutorial explaining how to create Pretty URL's within PHP yourself take a look at this tutorial from TutsPlus: http://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049
Hope I helped you! Good luck.
I am not good in english, but here i can try to answer.
look like your blog is on a sub dir.
so,
RewriteRule ^blog/([^/]*)$ /blog/?id=$1 [L]
and try to add
RewriteBase /
if you still have problem,
also..
there are many generator for htaccess
for example
http://www.generateit.net/mod-rewrite/index.php
I want to change dynamic URL's to URL's more acceptable by search engines.
For example change this :
http://myurl.com.au/page.php?id=100&name=myname
to
http://myurl.com.au/100/myname.php
or .html at the end it does not matter.
I am using Apache 2.2. I am not using .htaccess rather I put my code in /etc/httpd/conf/vhosts/myfile
but it does not work, the URL does not change at all.
Options Indexes Includes +FollowSymLinks
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.php$ /page.php?id=$1&name=$2 [L]
What am I doing wrong?
Either your have the wrong description in your question or your rule is backwards. Maybe this could work:
Options Indexes Includes +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} id=(.*)&name=(.*)$
RewriteRule ^/page\.php /%1/%2.php [L]
After doing some further testing it turns out that I do have the right code. I just don't have my head screwed on and was not thinking. Expecting that the mod_rewrite would magically change the URL to symbolic links when it is actually doing the reverse of that. It is all working for me now.
Generally a url that looks like this:
http://www.domain.com/product.php/12/
will open up product.php and serve the /12/ as request parameters, which then my PHP script can process to pull out the right product info. However when I migrated this whole site, after developing it, to a new server, I get a 404 error, because on that server it's not defaulting to the mother directory/file in case of an absence of requested directories.
I vaguely remember learning that this is generally a common apache function but I can't seem to recall how to set it up or how to manipulate it.. if there's an .htaccess method to achieve this that would be great.
What you're referring to is mod_rewrite. The official docs for it are here: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
You would configure it either in your VHost definition (recommended) or in an .htaccess file.
Assuming that you want to map all requests to a resource that Apache cannot serve (such as files that don't exist) to products.php you can use the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /products.php?request=$1 [NC,L]
You can then use $_GET['request'] to get the path requested and take it from there, depending on what you want to do. I'd normally recommend letting mod_rewrite handle parsing the request and passing the proper attributes to your PHP, but if you're not familiar with mod_rewrite it's probably easier to do it in your PHP.
you can use mod rewrite engine to map this to
http://www.domain.com/product.php?arg=12
Mod rewrite details: http://forum.modrewrite.com
Sample:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^files/([^/]+)/(.+) files.php?app=$1&file=$2 [NC]
this rewrite rule will map any request containing files/firstrPart/secondpart to the script files.php
everything between the first and second slash after files will be passed as parameter app and the rest as file
Basicly you define a regex with some subpaterns and state which script should really be called.
You cna refer to the subpatterns with $n where n is the 1 based index of the pattern.
Have fun.
NOTE this is a extreme simplification of mod rewrite. Please do some research before you use it because this might go terribly wrong...
The directive you're looking for is "AcceptPathInfo on". mod_negotiations MultiViews feature would also give you the option of not including the ".php" which is another common one people abuse mod_rewrite to do.
I have a content site that spreads across multiple pages but there is only 1 index.php file which retrieves the data from the database based on the page no.
Currently the url direction has to be something like domainname.com/Page/content.php?page=3
I have noticed, quite a few sites have directory like structure for the urls like:
domainname.com/Page/3
I know how to change it to domainname.com/Page/?page=3 but I am looking to remove the ?page part.
How can I do this, without individually creating a directory for each page, the content keeps growing, hence changes for each page.
Thanks
These sites use mod_rewrite to do a "rewrite" on the requested URL using a regular expression
EDIT:
rewriting this: domainname.com/Page/3
to that: domainname.com/Page/content.php?page=3
would look like this in the .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/(.*)$ /content.php/$1/page=$2 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /error_with_mod_rewrite.html
</IfModule>
here i also made a restriction for the var name to be a letter either capital or small.
"URL Rewrites" is the feature that performs a custom URL structure. This is a very common and important feature in most common web servers. URL rewrites are not just available with Apache. For example,
Apache uses the Mod_Rewrite Module.
Nginx uses the 'Rewrite' and "Try_Files" directives.
Cherokee has the feature built-in, configurable from admin web panel.
there are many more examples, these are just some I have worked with. In addition, I have heard some projects do the rewrites in the programming code. This has become more prevalent with the new language specific web-servers.