How to point a URL to a page? - php

I have seen in Wordpress, when you create a page say "Register", it points to http://example.com/register/ but without having a folder named register in the root at all. How is this done?
Its like this. When a user clicks the link on http://example.com/index.php which says Register, it takes the user to a new page and the URL in the browser would be http://example.com/register/ and the page loads. The register folder itself does not exist.
From the answers below I learnt that the request is passed through the index.php by modifying the .htaccess
I want to know what code to place in index.php so that http://example.com/register/ would display the register page.

In my Wordpress .htaccess file I have:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Which basically says if the "filename" part of the URL doesn't exist as a file !-f or exist as a directory !-d then rewrite the request internally to be /index.php which forces the request to be processed by index.php of Wordpress.
Ok, based on your additional comments, the following .htaccess rules will take http://domain.com/<anything> and internally rewrite it to http://domain.com/index.php?page=<anything>, this will be done without regard to case (NC flag) and will keep (pre-pend) any existing query string (QSA flag). This only does this for filenames and directories that do not exist on the server.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?page=$1 [NC,QSA,L]
This does have the following side effect which you should be able to handle programmatically, http://domain.com/register/ will be rewritten to http://domain.com/index.php?page=register/ which includes the trailing "/" since this is a directory reference. Again, you should be able to handle that in your PHP code.

When the web server, Apache in this case, attempts to open a requested file path, /register/ in this case, it first looks at the .htaccess file.
The .htaccess file among many other things can redirect the request to another path.
What usually happens is all requests redirect to a single file, index.php in WordPress, which reads the original request, /register/, and serves up the correct html.
What WordPress does to serve up the correct html is another question altogether but you can start here: https://www.google.com/search?q=how+wordpress+works

Wordpress uses mod_rewrite in .htaccess file as follows:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Which routes all page requests through index.php if the file or directory doesn't exist and the script is not index.php

Neil Martin, this is really not a simple question. You are better off reading some good material on URL REWRITING

Related

how to hide the complete path in php with htacces file

I have a project with the name of social app which contains has a structure like this:
includes
_login.php
_profile.php
pages
login.php
profile.php
currently if I want to vists any page like profile.php I have to visit localhost/social-app/pages/login.php but I want to modify it and change the url to localhost/social-app/login.php. basically I want to get rid of the pages and make my URL a bit cleaner
In your htaccess in the root , put the following :
RewriteEngine on
RewriteRule ^social-app/login\.php$ /social-app/pages/login.php [L]
Now instead of going to long URL you can just type /social-app/login.php to access the file in pages folder.
EDIT :
To remove the directory name for all files , you can use the following :
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/social-app/pages/$1.php -f
RewriteRule ^social-app/([^.]+)\.php$ /social-app/pages/$1.php [L]
If the rule above fails , then use this :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^social-app/(.+)\.php$ /social-app/pages/$1.php [L]
This will internally map a request for /social-app/filename.php to /social-app/pages/filename.php .
If everything (ie. images, CSS, JS and PHP pages) need to be rewritten to the /pages subdirectory then you can do something like the following in the /social-app/.htaccess file using mod_rewrite:
RewriteEngine On
RewriteRule !^pages/ pages%{REQUEST_URI} [L]
This unconditionally rewrites everything that is not already prefixed with pages/ to the pages subdirectory. eg. /social-app/login.php is internally rewritten to /social-app/pages/login.php.
If you have static resources in other locations that should not be rewritten then include a filesystem check to prevent requests that already map to existing files from being rewritten. For example:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^pages/ pages%{REQUEST_URI} [L]
If you are changing an existing URL structure then you'll need to redirect the old URLs that are indexed by search engines and perhaps linked to from third parties. For example, the following "redirect" would need to go before the above rewrite:
RewriteBase /social-app
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^page/(.*) $1 [R=301,L]

How can I rewrite a URL only if the page doesn't exist?

I have a web page at myWebsite.com and in the same directory a small number of other pages like myWebsite.com/about and myWebsite.com/contact.
I'm trying to make a user-profile system where if I go to myWebsite.com/user1 it redirects to the index but can use user1 as a variable (to pull their user account information from the database).
How can I do this only if the extension to the URL is not a page that actually exists?
Edit:
...Without hard-coding in the pages that do exist like /about and /contact
If your system uses physical files or directories for /about and /contact then you could use this common instruction to rewrite those that do not to index.php:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This simply checks if the requested page
IS NOT index.php
IS NOT an existing file
IS NOT an existing directory
then rewrite to index.php. Afterwards, you will be able to access /user1 via $_SERVER['SCRIPT_URL'] in your index.php file.
That said I agree with the comments that sending everything through index.php would be best, and the above snippet could get you going in that direction as well.

How to redirect static to dynamic URL's using .htaccess (on Wordpress site)

I've taken over a former site/domain, and set up a new site using Wordpress. The WP installation rewrites URL's to static ones, as you'd expect it to.
At the same time I want to preserve the former pages, as they have incoming links. I'm not interested in 301'ing them to "new" pages.
The old URL structure is /index.php?id=123, which I suspect is causing the problem with the WP .htaccess file. For reference, this is what it looks like:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I've tried adding the following:
RewriteRule ^([0-9]+).html index.php?id=$1 [R,L]
Doesn't work. Just redirects to site.com/?id=123 and shows the front page.
I should add that I plan on just adding these new pages as regular static HTML files in the format of 123.html, 321.html etc.
How do I use .htaccess to make this work together with the WP installation and what WP puts into the .htaccess file?
To clarify:
I want to have my 123.html static HTML page be index.php?id=123. When you access index.php?id=123 it should bring up 123.html, but show index.php?id=123 in the address bar. If you access 123.html it should 301 to index.php?id=123.
To map an URL with a querystring up to an actual file you'll need to use a RewriteCond to match the querystring itself (as RewriteRule doesn't):
Something along these lines ought to do it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# retrieve X.html when index.php?id=X is requested
RewriteCond %{REQUEST_FILENAME} index\.php
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteCond %1.html -F
RewriteRule .* %1.html? [L]
# standard WordPress routing
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This will first check to see if you've got a request for index.php with a querystring like id=X.
Then it'll check to see if a file called X.html actually exists; I'm not 100% happy about having to use the more system hungry subrequest file check -F rather than the standard -f but I can't see a way around it in .htaccess in this case.
If X.html actually exists, it'll fetch that file whilst leaving the URL as index.php?id=X.
However if that file doesn't exist it'll fall back to standard WordPress no file, no directory routing to index.php
I'm not a WordPress expert but that should work; I guess the main WordPress controller uses $_SERVER['REQUEST_URI'] to determine the action.
Note: This won't, however, prevent people from accessing 123.html directly by going to the URL www.site.com/123.html - I kept falling into infinite loops and Apache 500 errors trying to prevent that :|

How to deal with CSS, JS files when used with global apache redirect (for user friendly url's)

I have the following in my htaccess file:
RewriteEngine On
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
From rewrite.php I redirect to the correct pages depending on the url. Problem is that it redirects all files including css and js. I tried including these files but I now realise that was dumb of me. Should I redirect when there is and appropriate extension in the url? If redirecting is the way to go what method would be best? header location or HTTP_redirect?
Or is this not a good idea performance or work involved wise? I could go for something like this but I know next to nothing about apache and would rather not work with it right now.
RewriteRule ^(.*).css$ /includes/compressor.php?i=$1.css [L]
I previously had the following in my htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
I decided to remove this because:
I would not be able to include the header and other common files in the rewrite.php file. I would also not be able to have a database call in the rewrite file that would determine the page to include and to reuse the data for the page contents.
Unwanted files would be reachable such as service used only by external app.
The compression should be done once, and not for every request. You can then exclude requests from the URL rewriting if the corresponding file exists:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
How about redirecting only if the requested file does not exist on the server?
You could use the following rewrite conditions to achieve this.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
So if the request is for a CSS/JS/HTML/Image file that already exists on the server then no rewriting takes place and the request for the file is processed. If the file does not exist, it will run your rewrite rule to rewrite.php

Create dynamic subdirectories using htaccess and php

Every single time a user registers on my site I would like them to have their own subdirectory with their registered "username". Every user subdirectory will have the same "index.php" file which will do something.
For example: "/users/username1/" and "/users/username2/"
If some one wants to access the subdirectory they would simple go to:
"www.example.com/users/username1/" or "www.example.com/users/username2/"
The easy and messy solution would be to simply create a subdirectory for every user and place the same "index.php" file in every directory. But to me this is only going to crowd my server space and make my directories large.
I wanted to know if all this can be done using .htaccess? Can I create one "index.php" and one ".htaccess" file and place them both in my "/users/" directory? What would be the actual code that I would have to place in my .htaccess file??
If you have a better way of doing this please let me know. I am using Apache and PHP as my working environment.
Thank you
Well, for example, you could do it all with one htaccess like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
What it does:
switches on rewrite engine
checks if a requested file exists
checks if a requested directory exists
if NOT, it redirects request to your main index.php
Basically that means if you enter url such as yourdomain.com/users/ivan/, you request will be redirected to:
index.php?url=/users/ivan
then you $_GET['url'] in your index.php and split it into pieces.
That's just an example, there other mod_rewrite methods to do this.
Make it virtual. There are no subdirectories, you can use mod_rewrite to simulate that.
With mod_rewrite you can make /users/username1 lead to /users.php?user=username1 for instance. Everything is transparent for the client, he wont notice what is really happening.
By using something like this:
RewriteEngine On
RewriteRule ^([\-_0-9A-Za-z]+)$ index.php?a=$1 [L]
You can customize RewriteRule as much as you want.
You can essentially type in any directory you want, and it will be redirected to your index.php page.
If you want to make sure the existing directories are not redirected, do this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\-_0-9A-Za-z]+)$ index.php?a=$1 [L]
If you want to limit the scope, so only a subdirectory of user/ is redirected (similar to Stack Overflow), simply add in 'user' to the start of the rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([\-_0-9A-Za-z]+)$ index.php?a=$1 [L]
And finally, if you want to have an individual file handle all user requests seperate from your actual index.php page:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([\-_0-9A-Za-z]+)$ users.php?a=$1 [L]
This is a very similar setup I use to distribute CSS files.
Note: The Directory will be contained is $_GET['a']

Categories