6 Replies - 4716 Views - Last Post: 29 May 2012 - 06:08 PM

#1 Lazy Vulpes  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 02-May 12

Can't get url rewriting to work using apache.

Posted 22 May 2012 - 07:12 AM

It should be said that I'm both a newbie to php, apache and linux, which makes things more difficult for me, so please bare with me.

My .htaccess looks like this and is placed in the folder which I've made my websites root folder.
RewriteEngine on
RewriteRule ^test1.html$ test2.html



(I've also tried several other rewrite rules which have been said to work, but those didn't work for me ether)

I've checked if mod_rewrite is enabled, and it showed up under Loaded Modules using phpinfo()
I've checked both apache2.conf and httpd.conf in etc/apache2 but there was no mention of AllowOverride (I have actually no idea what AllowOverride does). It was however mentioned four places in /etc/apache2/sites-available/mysite (which I've edited to have "myusername/dev" folder as my root folder for my website), but changing them to "All" didn't do any difference.

I've read several forum threads, tutorial, etc, but nothing have worked. I'm writing this topic as my last option, if I can't get it to work with the help from you guys, I fear I have to give up using php entirely. :(

Please help!

Is This A Good Question/Topic? 0
  • +

Replies To: Can't get url rewriting to work using apache.

#2 Lazy Vulpes  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 02-May 12

Re: Can't get url rewriting to work using apache.

Posted 25 May 2012 - 01:06 AM

I've tested it on a external server and it works fine there, so it have for certainly something to do with my apache setup. Any help is greatly appreciated..
Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Can't get url rewriting to work using apache.

Posted 25 May 2012 - 01:36 AM

When you say "doesn't work", do you mean that it shows the wrong file, or do you get some sort of an error? Does it actually work to put, say, a test.php file into your web-root and having it print something?

Quote

I've checked both apache2.conf and httpd.conf in etc/apache2 but there was no mention of AllowOverride (I have actually no idea what AllowOverride does). It was however mentioned four places in /etc/apache2/sites-available/mysite...

Just to elaborate on which files you should be editing... The httpd.conf file is typically the "main" config file for Apache on Linux systems. Some distributions, like Debian and Ubuntu, may create a apache2.conf instead to replace or be joined with it. - Either way, editing these files causes server-wide changes.

The /etc/apache2/sites-available/ directory would list available virtual-hosts; which are separate sites hosted on the same server. Apache can host any number of sites on the same machine, each with their own configuration, port and IP assignments, and domain names. - Each file under that directory would hold a config file specific to one of those virtual-hosts, where you need to set things like the web-root location and whatever directives affect it.

Besides those "main" config files, you can define .htaccess files in the directory tree of your site, which will affect all the files below that point in the hierarchy. However, what is allowed to be changed using the .htaccess files is determined by the AllowOverride directive set for that location in your main, or host-specific, config file. - That is why you need to change it in order to enable mod_rewrite in .htaccess. The file needs permission to set the mod_rewrite directives, or Apache will ignore it.

Quote

... (which I've edited to have "myusername/dev" folder as my root folder for my website), but changing them to "All" didn't do any difference.

Could you show us the file that contained this? This would be the config file for the main virtual host, and it is important that this file be configured correctly in order for the .htaccess files to work.

Quote

My .htaccess looks like this and is placed in the folder which I've made my websites root folder.
RewriteEngine on
RewriteRule ^test1.html$ test2.html


One thing I'd point out in that RewriteRule. The . in the ^test1.html$ expression doesn't actually mean the character .. It represents all characters, so what you are actually saying there is: "The string 'test1', followed by any one character, followed by 'html'."

If you meant to match the actual . character, then you should escape it: ^test1\.html$.
Was This Post Helpful? 0
  • +
  • -

#4 Lazy Vulpes  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 02-May 12

Re: Can't get url rewriting to work using apache.

Posted 28 May 2012 - 11:15 AM

Quote

When you say "doesn't work", do you mean that it shows the wrong file, or do you get some sort of an error? Does it actually work to put, say, a test.php file into your web-root and having it print something?


It shows the wrong file. Yes, test.php with a echo("hello world"); works.

Quote

Could you show us the file that contained this? This would be the config file for the main virtual host, and it is important that this file be configured correctly in order for the .htaccess files to work.


<VirtualHost *:80>
	ServerAdmin webmaster@localhost

	DocumentRoot /home/username/dev/
	<Directory />
		Options FollowSymLinks
		AllowOverride None
	</Directory>
	<Directory /home/username/dev/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		Order allow,deny
		allow from all
	</Directory>

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all
	</Directory>


	CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>	ErrorLog ${APACHE_LOG_DIR}/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Can't get url rewriting to work using apache.

Posted 28 May 2012 - 11:32 AM

On line #11, you have AllowOverride None set for the /home/username/dev/ directory. That would be blocking your htaccess files in that directory. Try changing that to AllowOverride All and see if that changes anything.

Also, what is with the junk after the </VirtualHost> on line #36? As far as I can tell, that should be causing some major issues when you try to start Apache.
Was This Post Helpful? 0
  • +
  • -

#6 Lazy Vulpes  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 02-May 12

Re: Can't get url rewriting to work using apache.

Posted 29 May 2012 - 05:46 PM

Quote

On line #11, you have AllowOverride None set for the /home/username/dev/ directory. That would be blocking your htaccess files in that directory. Try changing that to AllowOverride All and see if that changes anything.


It worked! Thank you! :D

As stated in my original post I've actually already tried this.

Quote

It was however mentioned four places in /etc/apache2/sites-available/mysite (which I've edited to have "myusername/dev" folder as my root folder for my website), but changing them to "All" didn't do any difference.


However I was changing it to "All" at all the places AllowOverride was mentioned, maybe that was why, or perhaps I made some stupid mistake like a typo or something..


Quote

Also, what is with the junk after the </VirtualHost> on line #36? As far as I can tell, that should be causing some major issues when you try to start Apache.


I don't know, I haven't touched that part of the file.. :dontgetit: I don't get any critical errors when I start apache though..

Terminal said:

* Starting web server apache2
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[ OK ]

This post has been edited by Lazy Vulpes: 29 May 2012 - 05:51 PM

Was This Post Helpful? 0
  • +
  • -

#7 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Can't get url rewriting to work using apache.

Posted 29 May 2012 - 06:08 PM

View PostLazy Vulpes, on 30 May 2012 - 12:46 AM, said:

I don't know, I haven't touched that part of the file.. :dontgetit: I don't get any critical errors when I start apache though..

Hmmm. I suppose none of the directives there are technically invalid, except for the trailing </VirtualHost> which may very well be ignored.

Personally I'd remove them, or just comment them out in case it breaks something. It looks very much like it's some sort of copy/paste mishap that shouldn't be there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1