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

What I'm trying to do is set the RewriteBase to /DynamicCountry/DynamicName. Is this possible using Mod_Rewrite's variables?

  • 3
    RewriteBase is a directive that affects all relative path substitutions in the RewriteRule directive. To be honest I don't think this is really what you need. What exactly are you trying to achieve? I assume the "ugly" (real) URL is the one you have given (and which you would need to internally rewrite to)? And what does the "pretty" URL look like? The same but minus the file.php?? – MrWhite Jan 29 '14 at 23:16

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

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

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

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

$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';

  • 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:
    would show the page:

  • In the event of an HTTP 404 error, you'll need a file to be present at:
    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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.