I'm creating a simple php website with changeable themes. I have a directory called MyThemes with a a folder inside containing the following files header.php, sidebar.php, page.php, footer.php now the problem is that i want to display a page from the database using the page.php file of the selected theme, but the generated link will be something like
website/MyThemes/ThemeName/page.php?id=somePageID
I want to change that if possible to something like
website/pages/somePageID
I have a little experience with PHP, but apparently not enough to do this. So any help will be greatly appreciated.
This is typically done via URL rewriting by the webserver: the server makes sure /url/like/this is converted to (for instance) something.php?like=this. After that there is no difference to the application. Apache uses mod_rewrite to do this. If you were using Django, this would be configured in the urls.py files.
You could still simplify your URLs to website/pages.php?id=n by remembering the theme in a cookie or session variable though.
Also, your current file paths suggest that you duplicated the pages for the different themes though: this is never a good idea. Don't repeat yourself!
You can use friendly urls and regular expressions:
See
http://www.phpriot.com/articles/search-engine-urls
In this article, you'll find instructions for using htaccess:
Example:
<IfModule mod_rewrite.c>
RewriteEngine on
#rule not apply directories
RewriteCond %{REQUEST_FILENAME} !-d
#rule not apply files
RewriteCond %{REQUEST_FILENAME} !-f
#Rule to page
RewriteRule ^page/$ page.php
RewriteRule ^page$ page.php
RewriteRule ^page/(.*)/(.*)?$ page.php?id=$1&des=$2
RewriteRule ^page/(.*)?$ page.php?id=$1
</IfModule>
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]
I'm trying to figure out a way to load a template php file AS a requested url/filename:
I have a directory with hundreds of pages, and would like to be able dynamically generate them from a template file rather than have hundreds of files containing the same code. Ideally there would be one php file that loads content from MySQL using basename(), where every url requested from a particular directory would open /gallery/template.php AS the requested url, such as /gallery/example.html.
I feel like this can probably be done using .htaccess and mod-rewrite, but I haven't found an example of it in action. I'm trying to avoid using GET, but if there is a better way to achieve this effect, I'm open to suggestions. Thank you.
You might want to use AJAX as a method for accomplishing what you want. Are you familiar with AJAX? If you post more details about your specifics and desired outcome, perhaps we can help you further.
Review these simple AJAX examples, and see this video resource.
Take a look at $_SERVER['PATH_INFO'] on the $_REQUEST manual page. Using that, you can get something like
http://example.com/index.php/path/to/your/template
to call index.php passing along the entire /path/to/your/template as a string in $_REQUEST['PATH_INFO'].
And in your .htaccess file, you can do the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
(rewrite rules taken from here. I haven't done the mod-rewrite part myself, so I can't guarantee this will work).
This will take something like http://example.com/path/to/your/template and have the server treat it as http://example.com/index.php/path/to/your/template, making it pass everything to your index.php file.
After that, how you load your templates is up to you.
I haven't test it, but here is an example:
RewriteEngine On
RewriteRule ^gallery/.*/?$ gallery/template.php [NC,L]
String 'gallery' must be in the requested URL.
Basically, you are wanting to execute a front controller pattern. If everything in your gallery subdirectory needs to be redirected (i.e. there are no images files or subdirectories which don't need to be routed to template.php), you can do a simple RewriteRule to achieve this. Just place this in .htaccess or your apache .conf file
RewriteEngine On
RewriteRule ^/?gallery/.*\.html$ /gallery/template.php [QSA,L]
Ok i guess this is a bit of a general question really as opposed to a problem.
I am building a system where users can share their profile on other websites so i am wondering is it possible to shorten the actual url which would provide a link to their profile which would be something like this, www.somedomain.com/users/profile.php?user=myusername to simply cut out the users folder and the profile page and so something like this: www.somedomain.com/myusername
I have seen lots of url shortening scripts but they don't seem to do this, any suggestions or advice would be appreciated.
Thanks
What you're looking for is called URL rewriting and can be done using Apache's mod_rewrite. You would place a file called .htaccess in your root web directory and it would contain a snippet like this:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /(.*) /users/profile.php?user=$1
this is called "url rewriting" - there are different approaches to do this, for example using apaches mod_rewrite. another way would be to manually parse $_SERVER['REQUEST_URI'] - this would make your site work even if mod_rewrite isnt enabled, but is some more work.
I have a site which I have converted to use cms made simple. It works perfectly and I have the friendly urls working fine too. My issue is with a little script I wrote myself and how best to integrate it. The script is a gallery script, it reads a directory and outputs a formatted gallery in html. I was planning on making it a user defined tag in cms made simple but I hit a small snag.
The gallery script needs to be able to read in two values from the url groupId and showpage.
If I am using freindly urls then the cms and use the tag I hit a snag as the cms tries to find an actual page at "www.mysite.com/gallery/mygroup/2" and then throws a 404.
basically I need
http://www.mysite.com/gallery/photogroup/2
rewritten to
http://www.mysite.com/gallery.php?groupId=photogroup&showpage=2
UPDATE
Follwoing Yuri's advice I added his rule to the htaccess. But I have hit another snag.
So for instance if we go to
http://www.mysite.com/gallery/photogroup/2
then Yuri's rule should take effect. But that path is also a correct physical directory on my site coincidentally. Is there a way to have the rewrite rule take effect instead of bringing me to a white screen browsing the files in the directory or to the forbidden screen if I have indexes turned off which I do.
Below is my htaccess
php_flag magic_quotes_gpc Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1
RewriteRule ^gallery/(\w+)/(\d+)$ gallery.php?groupId=$1&showpage=$2 [QSA,L]
So, did you try to write in .htaccess something like this?
RewriteEngine On
RewriteCond ${REQUEST_FILENAME} !-f
RewriteCond ${REQUEST_FILENAME} !-d
RewriteRule ^gallery/(\w+)/(\d+)$ gallery.php?groupId=$1&showpage=$2 [QSA,L]
sounds like a "module" to me. Maybe this Make your module use clean URLs
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.