EzDevInfo.com

.htaccess interview questions

Top .htaccess frequently asked interview questions

How to debug .htaccess RewriteRule not working

I have a RewriteRule in a .htaccess file that isn't doing anything. How do I troubleshoot this?

  • How can I verify if the .htaccess file is even being read and obeyed by Apache? Can I write an echo "it is working" message, if I do write it, where would that line be echoed out?
  • If the .htaccess file isn't being used, how can I make Apache use it?
  • If the .htaccess is being used but my RewriteRule still isn't having an effect, what more can I do to debug?

Source: (StackOverflow)

Reference: mod_rewrite, URL rewriting and "pretty links" explained

"Pretty links" is an often requested topic, but it is rarely fully explained. mod_rewrite is one way to make "pretty links", but it's complex and its syntax is very terse, hard to grok and the documentation assumes a certain level of proficiency in HTTP. Can someone explain in simple terms how "pretty links" work and how mod_rewrite can be used to create them?

Other common names, aliases, terms for clean urls: RESTful URLs, User-friendly URLs, SEO-friendly URLs, Slugging, MVC urls (probably a misnomer)


Source: (StackOverflow)

Advertisements

Make .git directory web inaccessible

I have a website that I use github (closed source) to track changes and update site. The only problem is, it appears the .git directory is accessible via the web. How can I stop this and still be able to use git?

Should I use .htaccess? Should I change permissions of .git?


Source: (StackOverflow)

htaccess redirect to https://www

I have the following htaccess code:

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteCond !{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

</IfModule>

I want my site to be redirected to https://www. with HTTPS, and enforcing the www. subdomain, but when I access http://www. (without HTTPS), it does not redirect me to https://www with HTTPS.


Source: (StackOverflow)

How to manually create a file with a . dot prefix in windows for example .htaccess

I want to create a .htaccess file manually and discovered it seems impossible through the windows UI. I get a you must type a filename. message. There has to be a way to create files with . as a prefix in windows.

Can this be done manually?

enter image description here


Source: (StackOverflow)

.htaccess - how to force "www." in a generic way?

This will change domain.com to www.domain.com:

# Force the "www."
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

How do I replace the "domain" part so that this works on any domain?


Source: (StackOverflow)

How to remove .htaccess password protection from a subdirectory

I have password protected my entire website using .htaccess but I would like to expose one of the sub directories so that it can be viewed without a password.

How can I disable htaccess password protection for a sub directory? Specifically what is the .htaccess syntax.

Here is my .htaccess file that is placed in the root of my ftp

AuthName "Site Administratrion"
AuthUserFile /dir/.htpasswd
AuthGroupFile /dev/null

AuthName secure
AuthType Basic
require user username1
order allow,deny
allow from all

Source: (StackOverflow)

URL rewriting with PHP

I have a URL that looks like:

url.com/picture.php?id=51

How would I go about converting that URL to:

picture.php/Some-text-goes-here/51

I think WordPress does the same.

How do I go about making friendly URLs in PHP?


Source: (StackOverflow)

How to Set AllowOverride all

I want to set the AllowOverride all But I don't know how to do it. I have found the following code by searching the google and pasted it in .htaccess

<Directory>
        AllowOverride All
</Directory>

But after pasting it I started receiving "Internal Server Error"

Can Any One Guide Me Where to Put this code OR how to do it?


Source: (StackOverflow)

.htaccess rewrite to redirect root URL to subdirectory

Trying to get

www.example.com

to go directly to

www.example.com/store

I have tried multiple bits of code and none work. Please help!

What I've tried:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^(.+)\www.example\.com$
RewriteRule ^/(.*)$ /samle/%1/$1 [L]

What am I doing wrong?


Source: (StackOverflow)

Access-Control-Allow-Origin Multiple Origin Domains?

Is there a way to allow multiple cross-domains using the Access-Control-Allow-Origin header?

I'm aware of the *, but it is too open. I really want to allow just a couple domains.

As an example, something like this:

Access-Control-Allow-Origin: http://domain1.com, http://domain2.com

I have tried the above code but it doesn't seem to work in Firefox.

Is it possible to specify multiple domains or am I stuck with just one?


Source: (StackOverflow)

How does RewriteBase work in .htaccess

I have seen this in a few .htaccess examples

RewriteBase /

It appears to be somewhat similar in functionality to the <base rel='nofollow' href=""> of HTML.

I believe it may automatically prepend its value to the beginning of RewriteRule statements (possibly ones without a leading slash)?

I could not get it to work properly. I think it's use could come in very handy for site portability, as I often have a development server which is different to a production one. My current method leaves me deleting portions out of my RewriteRule statements.

Can anyone explain to me briefly how to implement it?

Thanks


Source: (StackOverflow)

Apache: client denied by server configuration

I am getting

[Tue Apr 24 12:12:55 2012] [error] [client 127.0.0.1] client denied by server configuration: /labs/Projects/Nebula/bin/

My directory structure looks like (I am using Symfony 2, should be similar structure for other web frameworks)

enter image description here

I have vhosts setup like:

<VirtualHost nebula:80>
    DocumentRoot "/labs/Projects/Nebula/web/"
    ServerName nebula
    ErrorLog "/var/log/httpd/nebula-errors.log"
</VirtualHost>

<Directory "/labs/Projects/Nebula/">
    Options All
    AllowOverride All
    Order allow,deny
    Allow from 127.0.0 192.168.1 ::1 localhost
</Directory>

I wonder whats the problem and how do I fix it?


Source: (StackOverflow)

Do you have to restart apache to make re-write rules in the .htaccess take effect?

I have pushed my .htaccess files to the production severs, but they don't work. Would a restart be the next step, or should I check something else.


Source: (StackOverflow)

How to redirect all HTTP requests to HTTPS

I'm trying to redirect all insecure HTTP requests on my site (e.g. http://www.example.com) to HTTPS (https://www.example.com). I'm using PHP btw. Can I do this in .htaccess?


Source: (StackOverflow)