PHP make subfile of a placeholder file? - php

I have the following structure for my site:
./u/
./u/profile.php
./u/comics.php
profile.php is filtered with the viewer's username. For example, the account Venk would be ./u/Venk. I need comics.php to somehow be a subfolder of this but I'm not sure how to go about doing so. It should end up looking like this: ./u/Venk/comics.
How can I get this to work?
P.s, I could just use AJAX to do this ./u/comics?username=Venk but that is just ugly and inconsistent and just overall makes my site's theme look messy so I'd much prefer an alternative.
P.p.s, Feel free the edit the post to make the title more concise, I wasn't really sure how to word it in basic terminology.

Create a .htaccess file in root with this content, make sure mod_rewrite is enabled.
You don't have to keep PHP files inside u directory, move profile.php and comics.php to root as well.
RewriteEngine On
# from: /u/{word}/comics
# to: /comics?username={word}
RewriteRule ^u/(\w+)/comics$ /comics.php?username=$1

Related

How to clean a url with htaccess?

I have a problem with url when loading PHP scripts.
The problem is that at the time of making the request to a php script, it loads normally, but when requesting another script, in the url they begin to gather and it looks like this:
www.example.com/file.php/route1/file2.php
I need this
www.example.com/file2.php
when i request another file, I need to have this
www.example.com/file2.php
What I need is to hide everything that it after file1.php or file2.php to load the other scripts without problems.
Without seeing your HTML content and .htaccess file it is hard to determine the exact cause of your issue(s).
Please verify the instructions in your .htaccess file. If you are using a Rewrite rule you need to validate that it is correct, for example:
RewriteRule ^/?$ "http\:\/\/example\.in" [R=301,L] If this rule is forcing a /route you obviously need to remove the /route from the instruction.
Please make sure to reference in your <a> tags the appropriate path. If you are not formatting it properly you will end up with concatenation. Are you using a framework? If so this may have an impact on your URL formatting.
Some example HTML a tags for you:
File 1 and File 1 will perform the same provided you do not have other factors impeding this simple approach.

how to use $_GET with this url format site.com/extension/rar

I am seeing this url format at most websites.
site.com/extension/rar
I wonder how they get the value='rar' using $_GET.
What I know is that $_GET can be use in here
site.com/extension/index.php?ext=rar
Now I wanted to change my way of calling a variable.
I wanted to apply what most websites do.
How can I call variable in the former?
Perhaps this works to get the "rar":
$name = basename($_SERVER['REQUEST_URI']);
I most likely being done using .htaccess
It is an Apache module that allows you "rewrite" urls at the engine level based on your own set of rules. So basically it rewrites URLs on the fly.
So, in your example you could have a file named .htaccess with the following contents: (there may be other options)
RewriteEngine On
RewriteRule ^extension/([a-z0-9]+)$ somefile.php?extension=$1 [L]
Basically, you are saying: If someone is looking for a URL that looks like "extension/somenumbers-and-letters" then show the contents of "somefile.php?extension=whatever-those-number-and-leters-are".
Do a search on Apache mod_rewrite to find more information.

Using .htaccess file for good looking urls

What I'm trying to do is make my website's urls look prettier to the users.
For example I have this link in the index.php file in a href tags:
index.php?v=class&id=5
And I want the user to see in the address bar this:
www.mysite.com/class/5
I have tried using this code:
RewriteRule /([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?v=$1&id=$2
But I found out that it does the opposite. It makes the good looking urls turn into bad ones. It would take the www.mysite.com/class/5 and show this: www.mysite.com/index.php?v=class&id=5. What should I do?
You're almost there.
Don't use R in your RewriteRule. R is an external redirect (it tells the browser to redirect, so the URL changes). Instead, remove the R to do an internal redirect, that way Apache can still parse the query string normally, but the user sees the pretty URL (the URL doesn't change in the browser).
Have you tried these yet?
Here's some links I found when seraching Google:
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
http://www.nouveller.com/quick-tips/quick-tip-6-how-to-write-clever-pretty-urls-with-htaccess/

SEO Friendly URL

I love the way SO gives link to question
Like this question have the link http://stackoverflow.com/questions/6002203/seo-friendly-url
where the question title is seo-friendly-url
I'm creating a blog where i want to give the link in the same way SO do, how to do that in PHP ?
Any suggestion is welcome :)
Table Structure
ID
Title
Tags
Category
UID
Added
I'm using PHP/APACHE and no framework ! I dont want to use any blog, want to create my own
I'm not sure why people are being so deliberately obtuse here...
What you are looking for is mod_rewrite, an apache module for URL rewriting.
In your .htaccess file (you might need to make it) put:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^blog\/([0-9]+)\/.*$ /blog.php?post=$1 [L]
</IfModule>
This means when you go to /blog/10/any-old-bit-of-text/ behind the scenes it is identical to if you visited /blog.php?post=10.
The ([0-9]+) bit is called a regular expression (or regex), and matches any number. The .* means match anything. The ^ anchors to the start of the query and the $ anchors to the end. slashes (/) are escaped as \/.
First of all you didn't write what framework do you user. I describe what you want to do on Symfony framework
First solution
http://www.symfony-project.org/jobeet/1_4/Doctrine/en/05#chapter_05_route_customizations
Change routes to routing engine understand the "Nice urls"
Change controller's action which is responsible for searching the right record with your's article
You can enhance the solution by declare sluging function and use it directly in routes
Second solution
Use any blogging solution which already supports it - as wrote ceejayoz
You could use PHP and Apache together. Specifically Apache Forcetype. This article explains how to use Forcetype.
Let's say you have a URL like this: http://www.example.com/article/seo-friendly-example
The .htacess file would look like this:
<Files article>
ForceType application/x-httpd-php
</Files>
The PHP would look something like this:
<?php
list(,$slug) = explode("/", $_SERVER['REQUEST_URI']);
?>
The value of $slug would be seo-friendly-example. This would be a key in your database for that article.

URL problems with swfAddress, mod_rewrite and SEO

Hey guys, I've got a full Flash website that's deeplinked and I'm working on a php script that will dynamically generate the alternate content on the page.
I can't get the SEO version of swfaddress to work so I'm trying to go around another way.
This is how it works so far - my crawlable links are like this:
mydomain.com?id=video -----> is rewritten as: mydomain.com/video/
The php script reads the id and prepares the appropriate content. Problem is, when users click on the flash content, their urls appear like this:
mydomain.com/video/#contact,
mydomain.com/video/#about-us etc etc
Which is confusing. Is there a way to get rid of "/video/" in the uri path with PHP or javascript? Any ideas or not possible?
To anyone who may be in the same position as me; struggling to understand Asual's swfAddress SEO solution I've reached a satisfactory conclusion.
I've used a single index.php and supporting datasource.php which contains all results in a switch statement. The switch statement is called first in the index.php and runs through collecting the following info from an id (eg. ?id=video) sent via GET. The switch statement then continues to match up appropriate content, setting variables for the rest of the script to use. I've used the following:
$title
$description
$keywords
$canonical // hint: -> 'domain/video?id=somevid' to 'domain.com/video/' (see canonical in google)
$stylesheet
$body
After the datasource.php, I include a "head.php" below it, which gets the html rolling, doctype etc (but is mostly concerned with the < head >) and lays out the keywords, description and content, specific to that page.
Swfaddress javascript will set the deeplinking upon entry for Flash users:
SWFAddress.setValue(< ?php echo "/$id" ?>); // eg."/video"
The index.php uses swfobject to embed - I use static embedding (I heard somewhere it was better for SE, more future proof but who knows, I prefer it though). The alternate content is a div, which echos the stored $body extracted earlier.
Finally I use mod_rewrite to setup my crawlable links. During testing I'm referencing my files directly.
< IfModule mod_rewrite.c >
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^home/? ?id=home [NE,L]
RewriteRule ^about/? ?id=about [NE,L]
RewriteRule ^video/? ?id=video [NE,L]
< /ifModule >
This ensures my links look like this: http://www.mydomain.com/video/
Which results in: http://mydomain.com?id=video
A search engine can crawl it perfectly, viewing only the html content where as users will see the flash, and when they click on the links they will see something like this:
http://www.mydomain.com/video/#/contact
As I said it's satisfactory, not as ideal as Asual's SEO solution, but clean enough. I couldn't find ANY information on swfaddress' SEO sample anywhere, and I certainly don't know enough php to dive into the vast uncommented code.
Oh, be sure to include this in the head of your index.php, it will ensure your links don't screw up when your swfs load external files.
< base href="http://www.mydomain.com/" />
Hope that helps someone at some point.

Categories