Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP CGI and mod_rewrite

Featured Replies

I'm making a website where the host is running PHP as CGI which is new to me.

 

I just uploaded the first test and ran into some problems.

 

I've set up www.example.com/feed/news.xml to point to www.example.com/feed.php?feed=news

On my local system that runs PHP on apache it works fine.

 

However, on the live server I get a "No input file specified" message when trying to access www.example.com/feed/news.xml

If I create a folder names 'feed' it works!

Why is this?

 

This is not a problem for the feed.

 

But I also have set up a news system where the URL's looks like this:

www.example.com/news/2007/05/title-text/ which points to www.example.com/news.php?year=2007&month=05&title=title-text

 

Because it's all dynamic I can't get away with just fudging it by creating dummy folders.

 

Any idea of why this is happening?

Hi Thomas,

 

Hmmmmm, sounds like it could possibley be that your host hasn't enabled Mod_Rewrite ?

 

You can do a check by doing the following:

 

Create a .htaccess file with the following:

 

Options +FollowSymLinks

RewriteEngine On

 

Save the file to a subdirectory of your main site something like this

 

/modtest/.htaccess

Now attempt to browse to the subdirectory (http://localhost/modtest/). One of two things could happen

 

- No errors, Congrats mod_rewrite engine is now enabled.

 

- 500, Internal Server Error If you get this message then mod_rewrite was not installed/enabled on your host.

 

You may want to ask your host if they can enable it.

 

Hope that helps.

  • Author

mod_rewrite is enabled.

other rewrite rules which rewrites .html files to .php files works.

 

 

I just added "cgi.fix_pathinfo = 1" to php.ini and that put an end to the "No Input" error messages. But I still get an 404 trying to access the feed unless I have a 'feed' folder in the root.

 

 

Hi Thomas,

 

Hmmmmm, sounds like it could possibley be that your host hasn't enabled Mod_Rewrite ?

 

You can do a check by doing the following:

 

Create a .htaccess file with the following:

 

Options +FollowSymLinks

RewriteEngine On

 

Save the file to a subdirectory of your main site something like this

 

/modtest/.htaccess

Now attempt to browse to the subdirectory (http://localhost/modtest/). One of two things could happen

 

- No errors, Congrats mod_rewrite engine is now enabled.

 

- 500, Internal Server Error If you get this message then mod_rewrite was not installed/enabled on your host.

 

You may want to ask your host if they can enable it.

 

Hope that helps.

  • Author

I just found out that the site by default, without any .htaccess files, redirects URLs like www.example.com/test/ into www.example.com/test.php

 

So when I request www.example.com/news/2007/05/title-text/ it points to www.example.com/news.php but NONE of the arguments are being passed on. the $_GET superglobal is empty. :( what gives?

I just found out that the site by default, without any .htaccess files, redirects URLs like www.example.com/test/ into www.example.com/test.php

 

So when I request www.example.com/news/2007/05/title-text/ it points to www.example.com/news.php but NONE of the arguments are being passed on. the $_GET superglobal is empty. :( what gives?

 

the super globals cant pass, as the URL needs to be bengribbin.com/news.php?ben=cool&&me=helpful

 

If the URL is rewritten, I am not sure it can GET the info from the URL

mod rewrite doesn't affect the $_GET variables, you can still use them in the way you want as long as your mod rewrite rules are correctly written.

 

Wildo

I wondered the same before Gold_Killer set me straight. Unless it was you wildo.

then you should contact your host with the problems you are getting, if you're host is a bit decent they will help you out for sure.

 

Wildo

  • Author

Yea, seem like that's what I'll have to do.

Problem is that it's a community site and I don't have control over it myself. I'm just the code monkey. Otherwise I'd get a different host which'd fit better.

Have you got the rewrite base set up in your htaccess?

RewriteBase /domains/yoursite/html

 

This is only an example but you need to tell the engine wher to rewrite from...

 

Oli

  • Author

I set RewriteBase to / thinking it'd be the root of my domain. Is that not the case?

 

the thing is, rewrites for .html files to .php files work. ...hm... maybe that is because they get inherited....

 

ok, this my .htaccess file:

 

Options +FollowSymlinks
#Options -Indexes
#IndexIgnore *.php *.inc
RewriteEngine on
RewriteBase /

#Directory Index index.php

# Deny access to the .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>

# Hide the php directory and php files
RedirectMatch 404 /\php(/|$)
#RedirectMatch 404 ^(.*)\.php$
#Alias / /php
#AliasMatch ^/icons(.*) /usr/local/apache/icons$1
#AliasMatch ^/(.*\.php) /server_root/php/$1
#AliasMatch ^/(.*\.php) /php/$1

# Redirect desktopx.info to www.desktopx.info
RewriteCond %{HTTP_HOST} (^desktopx\.info$) [NC]
RewriteRule ^(.*)$ http://www\.desktopx\.info/$1 [L,R=301]

# Redirect index files to /
RedirectMatch 301 ^(.*)index\.htm$ $1
RedirectMatch 301 ^(.*)index\.html$ $1

# Redirect htm files to html
RedirectMatch 301 ^(.*)\.htm$ $1.html

# Root rewrite
RewriteRule ^default\.html$  /index.php
RewriteRule ^register\.html$ /register.php
RewriteRule ^activate\.html$ /activate.php

# RSS rewrite
RewriteRule ^feed/news\.xml$ /feed.php?feed=news

# Admin tools rewrite
RewriteRule ^admin/default\.html$   /admin/index.php
RewriteRule ^admin/site\.html$	  /admin/site.php
RewriteRule ^admin/news\.html$	  /admin/news.php
RewriteRule ^admin/tutorials\.html$ /admin/tutorials.php
RewriteRule ^admin/snippets\.html$  /admin/snippets.php
RewriteRule ^admin/setup_db\.html$  /admin/setup_db.php

# News rewrite
RewriteRule ^news/$									 /news.php
RewriteRule ^news/(\d\d\d\d)/$						  /news.php?year=$1
RewriteRule ^news/(\d\d\d\d)/(\d\d)/$				   /news.php?year=$1&month=$2
RewriteRule ^news/(\d\d\d\d)/(\d\d)/([a-zA-Z0-9_\-]+)/$ /news.php?year=$1&month=$2&title=$3

# Todo
RewriteRule ^users/	 /todo.php
RewriteRule ^tutorials/ /todo.php
RewriteRule ^snippets/  /todo.php
RewriteRule ^projects/  /todo.php
RewriteRule ^forum/	 /todo.php

 

The test site is: http://testsite.desktopx.info/

 

(and I don't get why it keep redirecting http://testsite.desktopx.info/ to http://testsite.desktopx.info/default.html ... grrr)

  • Author

Got it working somewhat. But there are things I don't understand:

 

For some reason this worked on my local test server, but not on the host:

# News rewrite
RewriteRule ^news/$									 /news.php
RewriteRule ^news/(\d\d\d\d)/$						  /news.php?year=$1
RewriteRule ^news/(\d\d\d\d)/(\d\d)/$				   /news.php?year=$1&month=$2
RewriteRule ^news/(\d\d\d\d)/(\d\d)/([a-zA-Z0-9_\-]+)/$ /news.php?year=$1&month=$2&title=$3

 

I had to use this on the live server:

RewriteRule ^news/$										 news2.php
RewriteRule ^news/([0-9]{4})/$							  news2.php?year=$1
RewriteRule ^news/([0-9]{4})/([0-9]{2})/$				   news2.php?year=$1&month=$2
RewriteRule ^news/([0-9]{4})/([0-9]{2})/([a-zA-Z0-9_\-]+)/$ news2.php?year=$1&month=$2&title=$3

 

 

 

The news2.php bit was so I could work around the Content Negotiation.

And that's another thing. I tried placing the news.php file in a folder named 'php' so the rules would look like this:

RewriteRule ^news/$										 php/news.php
RewriteRule ^news/([0-9]{4})/$							  php/news.php?year=$1
RewriteRule ^news/([0-9]{4})/([0-9]{2})/$				   php/news.php?year=$1&month=$2
RewriteRule ^news/([0-9]{4})/([0-9]{2})/([a-zA-Z0-9_\-]+)/$ php/news.php?year=$1&month=$2&title=$3

 

But that resulted in a 404. The PHP folder permissions are 755.

At the top of the .htaccess I set RewriteBase /

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.