GET, URL-Rewrite - php

Continued from my previous thread: GET, if, elseif
I'm using a CMS but it has URL rewrite somewhere through the files.
How would I go around getting the $_GET to work without the .php extension?
Options +MultiViews
That still won't let me visit /something.php it says not found. (Would it be because the pages aren't in /, they are in another directory using a template system?

If you are using apache HTTP server, then you should consider to use multiview option.
No need to use RewriteRule there.
In the htaccess file, add :
Options +MultiViews

Related

Why is my .htaccess code is not working?

My htaccess file code is not working even it is right i found on many website for this each website has same code here it is :-
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^products/([a-zA-Z]+)/([0-9]+)/$ index.php?action=$1&sub_cat=$2
now this thing is not working www.example.com/products/something/3/
anything else i am forgetting please help me.
Because you have a products.php file and your URL looks like www.example.com/products/something/3/, a module called "mod_negotiation" is processing the request before mod_rewrite can. The Multiviews option will allow mod_negotiation to try to "guess" what a request is for, and it sees /products/ in the URL and the file /products.php and assumes that's what the request is for, serves the request via the products.php script and mod_rewrite never gets a chance to do anything.
Solution?
Turn off multiviews:
Options -Multiviews
by adding that option anywhere in your htaccess file.

How to change url in PHP (Static pages)

I am new to PHP. I am developing a website (as of now with static content, no database involved yet).
The url is seen like this for e.g : localhost/main/listcontent/content1#abc.php
I want to hide the entire path and replace it with something like : localhost/main?sub=1&brch=1
Any suggestions?
P.S : I have googled it but couldn't understand how to proceed.
Thanks in advance !
One way would be with a .htaccess rule:
RewriteEngine on
Options +FollowSymLinks
RewriteBase /
RewriteRule !(\.xpi|\.xml|\.txt|\.air|\.exe|\.zip|\.pdf|\.ico|\.gif|\.jpg|\.png|\.jpeg|\.js|\.swf|\.css|\.php|\.html)$ index_mod_rewrite.php [L]
Inside index_mod_rewrite.php you resolve the current URL to a mapping created by you (see $_SERVER["REQUEST_URI"]), and then require the necessary .php files.
Another would be to manually (or using a script) add all mappings to .htacccess.
The above is valid if you are using Apache. .htaccess is a file you place in any folder, and it affects the current folders and subfolders. Subsequent .htacccess files along the path can alter the configuration or even stop mod rewrite if you so choose for a particular folder.
For the sake of making sure, I am also adding here another requirement for the up most .htaccess file (security feature to disable directory listing):
options All -Indexes
IndexIgnore *

Domain/URL Masking

I have a website that passes some GET variables to different pages in PHP. My issue is that now I have a url with variables i.e. index.php?category=categoryname and that's not very memorable.
Is there any way I can change the URL to something like /categoryname instead without duplicating the page and storing in folders? But also allow users to type in /categoryname and be redirected to the correct page?
.htaccess Apache mod_rewrite, almost every professional dynamic website uses this method (like stackoverflow).
The method is fully explained in this article far better then I could ever explain it in this answer box.
You should look into writing some apache Mod_Rewrite rules in a .htaccess file.
The solution is discussed here:
this is done by the rewrite module of apache and this handles regular
expressions. You have to put a rule
like this in your .htaccess file on
the root of your website:
RewriteRule ^cat/([0-9]+)$
/index.php?category=$1
^ means the start of the url after
www.example.com/ $ means the end of
the page.
www.example.com/cat/123
will be converted by the server to:
www.example.com/index.php?category=123
In PHP you use the normal $_GET['id']
variable. The rewrite module must be
enabled by apache... This is mostly
used to make the url format
independent of the serverside
scripting language so the .php in the
url is not logical. Thats why i
changed it to product/ . The .htaccess
starts with
RewriteEngine On Options
+FollowSymLinks RewriteBase / Here all the rewrite rules.. ...

Removing .php extension with mod_rewrite and SEO issues

I'm trying to use modrewrite to change my urls from /foo.php to /foo and from /foo.php?lang=en to /en/foo. The problem I have is that I think I need to use 301 redirect to move the .php-less address to the .php address, or else my ranking will be splitted among these 2. But if I use [R=301] the address in the bar changes to the .php one making my pretty url efforts quite useless. What should I do?
I've looked around for any question\tutorial i could find but I can't fully understand modrewrite. The main issue I have is that if I change my .htaccess file then revisit an already visited page the new .htaccess is not working but is somehow caching the old result making correction a real pain. Do you know a workaround?
Thank you,
Mokuchan
Sounds like MultiViews would be a better solution than rewriting the URL. Just put this in your .htaccess. What that will do is essentially, if i type in stackoverflow.com/pages it will first look for a directory named pages, if it can't find it, a file. You can then have stackoverflow.com/pages/view/some/page where /view/some/page is the query.
Options Indexes FollowSymLinks Includes MultiViews
Edit To get the /view/some/page/ use $_SERVER['PATH_INFO'];
You definitely must look at Apache modul mod_rewrite and general idea of rewriting urls.
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

Running PHP without extension without using mod_rewrite?

Using Apache 2.2 and PHP 5, what's the best way to run PHP without the .php extension? For example, I have a script called app.php and I like to invoke it as:
http://example.com/app
Please notice that I still want to keep the .php extension to the file and I don't have mod_rewrite. Don't want use index.php either because it requires too many directories.
I did find one way by adding this to my .htaccess,
AddHandler server-parsed .php
SetHandler application/x-httpd-php
AddHandler application/x-httpd-php .php
The page runs a little slower by using this. I suspect it invokes SSI on every PHP page. Wonder if there are any better ways to accomplish this.
An alternative is to use content negotiation. Turn on multiviews:
Options +MultiViews
If a named resource doesn't exist, Apache will glob for the file, then sort based on the media type and content encoding requirements send by the browser. If there's only one file (your PHP script), then that's what the URL resolves to.
You could also force the mime type of a specific file in your .htaccess:
<Files app>
ForceType application/x-httpd-php
</Files>
The short answer is that you aren't going to be able to do this the way you want to. PHP is going to require SOME extension in order to execute the files so you might as well leave it as *.php.
Unless you use mod_rewrite you are going to have to call the files using the full file and extension.
That is the beauty of mod_rewrite--it lets you do just such a thing.
I would pose the question back to you--why can't you use mod_rewrite? Is it an environment issue, a choice, are you using Lighttpd (lighty)?
Extension
Wrap your rewrite rules in something like this to keep it from blowing up if the server doesn't support mod_rewrite:
<IfModule mod_rewrite.c>
// DO REWREITE HERE
</IfModule>
If you believe it to be a security concern or something equally valid then you could also do the following check and send them to a custom 404 or even your documentation and give them examples of how to enable mod_rewrite and explain the need, etc. This second check is optional though--if you don't use it the users will simply see the .php extension but your pages should still work.
<IfModule !mod_rewrite.c>
ErrorDocument 404 /application/errors/404.php
// ERROR 404 ABOVE OR DO ANOTHER DIRECT REDIRECT TO AN INFORMATION PAGE
</IfModule>

Categories