Category Archives: .htaccess-regex

Redirect if no above condition is matched in htaccess

I am redirecting all the pages of my old domain to a similar page of new domain and it's working fine but what I also want is that if the user is trying to access some other page which is not redirecting then it should automatically redirect to the homepage of the new domain.

Here is part of the file

RewriteEngine On 
RewriteBase / 
Redirect 301 /about-us/ 
Redirect 301 /contact-us/ 

It's working perfectly but how do I redirect if user tries to access any other page which is not mentioned above? I tried by adding the code below to the end of the file but it started redirecting every url to the home page

RewriteCond %{HTTP_HOST} !$ [NC]
RewriteRule ^(.*)$ [L,R=301]

How can I make ignore path with slash in PHP? [on hold]

Is there any way to get url like or in one file (for example index.php located in root)?

I want to get these path ad/da or xx/zz and put into follow code located in root folder (index.php).

echo file_get_contents(''.$upper_path);

In other word I want when user go to the URL I fetch ad/da and put into the follow code

echo file_get_contents(''.$upper_path);

Trying to shorten URLs using .htaccess, but hitting configuration errors

Brief intro: I'm attempting to set up a short URL system using a database and php. I've however came across a much shorter/quicker approach using a .htaccess file, regex, and a local config file. This tutorial however doesn't offer much support or explanation, and it's the first instance of URL re-writing I've seen.

Here's the code for the php script that gets the arguments passed into the page:


$links = parse_ini_file('links.ini');

if(isset($_GET['l']) && array_key_exists($_GET['l'], $links))
    header('Location: ' . $links[$_GET['l']]);
    header('HTTP/1.0 404 Not Found');
    echo 'Unknown link.';


So in other words it checks to see if the argument passed is in the static config file (example here would be google:, and if so, then of course re-directs them to the site. Configuration file contents:

google  =
fb      =

Fairly logical. Can understand how it works. My problem however comes with the .htaccess file as mentioned above. I've only dabbled with regex, and plan to learn more. However currently I'm not skilled enough to really understand the syntax required to write what is needing to be achieved. What needs to happen is for the user to basically type in and for them to be re-directed to - this is something I've been unable to get to work, and I just receive web server config errors. Config file for .htaccess:

RewriteEngine On

RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php?l=$1 [L]

Can anyone advise on the cause of the issue? Sorry for the lack of understanding on the regex part. I'm trying to skill up on it. I'd just like to get the fix in place so I can continue on my project, as it's only a small part of it.

Thanks a lot.