Mobile app version of vmapp.org
Login or Join
Smith883

: Using RewriteBase in .htaccess to make links pretty I'm working on a project right now and I'm trying to make the links pretty. We have urls like this: http://subdomain.domain.com/DynamicCountry/DynamicName/file.php

@Smith883

Posted in: #CleanUrls #Htaccess #ModRewrite #Url

I'm working on a project right now and I'm trying to make the links pretty.

We have urls like this: subdomain.domain.com/DynamicCountry/DynamicName/file.php
What I'm trying to do is set the RewriteBase to /DynamicCountry/DynamicName. Is this possible using Mod_Rewrite's variables?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Smith883

1 Comments

Sorted by latest first Latest Oldest Best

 

@Cugini213

Provided you are happy with pretty URL's that do not end in / but have no file extensions shown such as .php, for example:
subdomain.domain.com/DynamicCountry/DynamicName/file then you could try this solution.


Add these lines of code in your subdomain.domain.com/.htaccess file:



RewriteEngine on
RewriteRule ^(lib|theme|uploads|images|ajax) - [L]
RewriteCond !^(index.php|api|theme|images|robots.txt|sitemap.xml)
RewriteRule ^(.*)$ /index.php/ [L]



And then these in your subdomain.domain.com/index.php file (substituting the $sPath variable value with the correct linux path for your hosting environment):



<?php
$sPath='/var/www/subdomain.domain.com/';
$sAlias=isset($_SERVER["REQUEST_URI"])?substr($_SERVER["REQUEST_URI"],1):'';
if(strpos($sAlias,'?')>0) $sAlias=substr($sAlias,0,strpos($sAlias,'?'));
if(strpos($sAlias,'?')<1) $sAlias='default';
if(!file_exists($sPath.$sAlias.'php')) $sAlias='file-not-found';
require_once($sPath.$sAlias.'php');



And then create a pages sub-folder inside the subdomain web root folder, and inside there you could have sub-folders DynamicCountry and DynamicName and then file.php.
In this example, the pretty URL: subdomain.domain.com/DynamicCountry/DynamicName/file would show the page:
/var/www/subdomain.domain.com/pages/DynamicCountry/DynamicName/file.php
In the event of an HTTP 404 error, you'll need a file to be present at:
/var/www/subdomain.domain.com/pages/file-not-found.php
in order to ensure an infinite loop of File Not Found errors does not occur! For SEO reasons mainly, remember to use PHP code header('HTTP/1.0 404 Not Found'); before any HTML code is output. This helps search engines identify real pages from error pages.


Obviously you can modify this solution to your requirements but hopefully it gives you a good skeleton concept to work from.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme