Wordpress .htaccess Modification - php

I have a site with this structure:
Home
About Us
Products
Product
Services
Contact
If I open
mysite.com/products
reads page-products.php
and
mysite.com/product
reads page-product.php
I need to use page-product.php as a generic page.
For Example...
mysite.com/product/The-Best-Product/32
read page-product.php?name=The-Best-Product&id=32)
And how about
mysite.com/john/
and reads page-user.php?username=john
Its possible to make something like that works?
Thanks!

Consider using the Apache Web server's mod_rewrite, which is a very powerful tool to do just what you need. This would only help you if you are using Apache. Also, if you are not the Webmaster, mod_rewrite must have been enabled for use in .htaccess files.
The bottom of the page I linked has some examples on how to use this. Also, there are many tutorials around.

Related

Is there a possible solution on how to hide file name in PHP or .htaccess?

I want to change my url path to this:
localhost/sampleproject/validation.php
to
localhost/sampleproject/
I want to secure my file path. is there a code in .htaccess? if there is I will create .htaccess but i have no clue where to start, still learning to php thank you
that type of change usually hides index.php if you need to run code in a validation.php i might suggest including it within your index.php you can do so conditionally if necessary using include() or require_once() if you do that the user will have no knowledge of that file on the fronted. here's some examples of that https://www.w3schools.com/PHP/php_includes.asp
reference the links in comments of your question but as I said generally with apache you can configure it to not show the index.php but other than that it's per folder basis.
you may also want to look into creating some routes to trick the browser abit if that's your approach.
here's an example http://blogs.shephertz.com/2014/05/21/how-to-implement-url-routing-in-php/
or you can always use a framework such as symfony or Laravel which have route implementations for you to use.
Happy Coding.

Dynamic directories and files like wordpress

In wordpress there are directories which actually do not exists.
like example.com/post/name-of-the-post actually opens a main php file which fetches the content of that posts from database.
How can i implement this natively on linux apache php server without wordpress?
You need to use .htaccess. This is the technology behind Wordpress' ability to do what you are wanting to do.
Here are the docs from apache.
Also here's a nice Tutorial
I don't know why people use mod_rewrite, it is overkill for most tasks. For most things there are more nimble modules available. That is not to deny mod_rewrite, it has its place, but I find it is over used. In this particular case you can use mod_alias, no need for mod_rewrite. E.g.
AliasMatch ^/post/(.*) /real/path/post.php$1
module docs here: https://httpd.apache.org/docs/2.2/mod/mod_alias.html
This doc beautifully outlines when NOT to use mod_rewrite: http://httpd.apache.org/docs/current/rewrite/avoid.html

How to make use mod_rewrite to create pretty permalinks for a custom-built CMS PHP script meant for external distribution?

I am trying to build a CMS that will be used by other people, which means that I can't rely on editing the .htaccess files for mdo_rewrite. I have looked up methods for mod rewriting urls from http://example.com/index.php?category=food to http://example.com/category/food but most of them seem to rely on apache and editing the .htaccess file, which would only work if you are creating a script which, like public scripts like Wordpress, rely on being installed on an external user's server. How do I mod_rewrite from within the script so that the script either automatically edits the user's mod_rewrite settings in .htaccess (as in altering the rewrite rule in the file from within the script itself) or some other method that would work fo a CMS script that is solely written in PHP and intended to be used on another server.
If the script you are using, is making use of a popular framework, chances are they have installed a so called "routing" system inside the framework. That by itself allows nice URLs.
In fact, if you're using any commercial script for a webshop or something alike, they probably have a mod, plugin or component that allows nice URLs. Look it up or add additional info please. :-).

Website structure / Newbie

I created a website using php, passing values from page to page either with POST or GET.
Though there is some cons, I dont know how to track specifically what data has been viewed in GoogleAnalytics as it just shows the name of the page (xxxx.php)
On the other side, I see websites structured differently with a bunch of subdirectories created : www.xxx.com/xxxxxx/xxxxx/xxx
This looks like pretty manual for me , compared to the .php?xxxx=xxxx way of structuring.
Do you know how this subdirectory style structuring can be automatically obtained?
This is done with Apache rewrite rules.
To make it so that whenever a user goes to /posts/visiting_new_york, it actually goes to to /viewpost.php?id=visiting_new_york, you create a file in your site called .htaccess like this:
RewriteEngine On
RewriteRule '^posts/([^/]+)$' viewpost.php?id=$1 [L]
Use an MVC framework like rails, or simply configure your webserver's virtual directory structure to be identical to the local servers file system and adhere to that scheme when saving your php files.
Yes, you can do this with "mod_rewrite" in apache.
It involves creating a .htaccess file with URL re-writing rules inside.
So you can transform /index.php?page=contact&lang=en into /en/contact/
Here's a good rewrite cheat sheet: http://www.addedbytes.com/cheat-sheets/mod_rewrite-cheat-sheet/
Wadih
You need to read about url rewriting
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
If you just want to track your dynamic pages , there is another solution in Google analytic
http://www.google.com/support/googleanalytics/bin/answer.py?answer=55504

Rewrite index.php in Wordpress

I started a blog and when I changed my permalink structure to /%postname%/ I get a Page Not Found error. I want my url to look like this:
http://bobbybeckner.com/sharepoint-list-and-linq-using-jquery-and-ihttphandler/
not this:
http://bobbybeckner.com/index.php/sharepoint-list-and-linq-using-jquery-and-ihttphandler/
I read a few posts about changing .htaccess but found no clear solution. Any code examples welcome or recommendations on wordpress plug-ins would be greatly appreciated.
Update
I thought it would be important to mention that my host is running IIS7 but does not allow users to touch it. In addition, I'm uncertain of any restrictions on the .htaccess file or any other configuration limitations.
.htaccess files only apply to Apache (well, maybe some other servers use it too, but not IIS). AFAIK URL rewriting for IIS is possible, but not as easy.
ISAPIRewrite appears to be a commercial application that does this, but of course, you'd need to get it installed on your server.
Personally, I wouldn't lose any sleep over having /index.php/ in your urls.
Generally when you fill out the structure, if the .htaccess file is writable it will apply the rewrite for you, and if it isn't it will show what the contents should be at the very bottom of the page. Did you check the bottom of the page if it is not writable?
You do NOT have to code this yourself.

Categories