php - Mod-rewrite.htaccess not working

794

Hi I previously posted a question on to Stack Over flow and was lucky enough to receive some assistance to my initial question which helped me better understand that what i was trying to achieve was possible.

The previous question is here: Visit php url shortening

I am building a system where users can share their profile on other websites so i am wondering is it possible to shorten the actual url which would provide a link to their profile which would be something like this, www.somedomain.com/users/profile.php?user=myusername to simply cut out the users folder and the profile page and so something like this: www.somedomain.com/myusername

the code I have is:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule  /(.*) /users/profile.php?user=$1

Sadly two things happen, firstly the code doesn't work and secondly when i add this code the css on the original long domain stops working.

Any suggestions would be appreciated

221

Answer

Solution:

code doesn't work: of course it won't because it's incorrect. Really wondering how did you end up accepting other question without properly testing it. Anyway take this code and replace your existing .htaccess code:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php/?$ - [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule  ^(.*)$ users/profile.php?user=$1 [L,QSA,NC]
927

Answer

Solution:

This is serverfault question.

  1. Are your running apache?
  2. Are apache mod_rewrite module enabled?

    debian: a2enmod rewrite; /etc/init.d/apache2 restart

  3. Are enabled override for this directory? In case using default vhost:

    <Directory /var/www/>
            #AllowOverride None
            AllowOverride FileInfo
    </Directory>
    
  4. What are showing error.log? tail /var/log/apache2/error.log

  5. Try simple version:

Code:

RewriteEngine On
RewriteBase /
RewriteRule  ^profile.php profile.php [QSA,L]
RewriteRule  ^(.*)$ profile.php?user=$1 [QSA,L]

People are also looking for solutions to the problem: php - Setting up zend framework

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.