I am using URL rewrite on Winwdows IIS and Linux Apache to get better looking URL-s. The URL rewriting itself works perfectly, but if I want to access some folders on that domain or in the folder where URL-s are rewritten, web-server rewrites the folder name as well and sends the request to my referred php file.
For example if I have mydomain.com/somesite/about, it becomes mydomain.com/somesite/index.php?one=about. But if I want to access a folder called "somefolder" (that actually exists) at mydomain.com/somesite/somefolder, then the only way is to refer to an exact file in that folder (mydomain.com/somesite/somefolder/index.php), because otherwise it is being rewritten to mydomain.com/somesite/index.php?one=somefolder
Could someone please give me an example on how to get the URL-s rewritten and as well access folders without referring to specific files.
Here is my Apache .htaccess:
<IfModule mod_rewrite.c> RewriteEngine on #RewriteCond %{REQUEST_FILENAME} !-f #RewriteCond %{REQUEST_FILENAME} !-d Rewriterule ^([\\w\\-]+)/?$ index.php?one=$1 Rewriterule ^([\\w\\-]+)/([\\w\\-]+)/?$ index.php?one=$1&two=$2 Rewriterule ^(\\w+)/(\\w+)/(\\w+)/?$ index.php?one=$1&two=$2&three=$3 Rewriterule ^(\\w+)/(\\w+)/(\\w+)/(\\w+)/?$ index.php?one=$1&two=$2&three=$3&four=$4 </IfModule>
And here is my IIS web.config:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <clear /> <rule name="one" enabled="true"> <match url="^(\\w+)/?$" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false" /> <action type="Rewrite" url="/somesite/index.php?one={R:1}" /> </rule> <rule name="two" enabled="true"> <match url="^(\\w+)/(\\w+)/?$" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false" /> <action type="Rewrite" url="/somesite/index.php?one={R:1}&two={R:2}" /> </rule> <rule name="three" enabled="true"> <match url="^(\\w+)/(\\w+)/(\\w+)/?$" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false" /> <action type="Rewrite" url="/somesite/index.php?one={R:1}&two={R:2}&three={R:3}" /> </rule> <rule name="four" enabled="true"> <match url="^(\\w+)/(\\w+)/(\\w+)/(\\w+)/?$" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false" /> <action type="Rewrite" url="/somesite/index.php?one={R:1}&two={R:2}&three={R:3}&four={R:4}" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
Thanks guys!
If you want to do SEO friendly url, I suggest to develop a routing component, next step how to to this.
You should use this rewrite configuration
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [QSA,L]
RewriteRule ^(.*)$ /index.php [QSA,L]
I explain this line step by step
RewriteCond %{REQUEST_FILENAME} -f
condition : all requests that matches an existing regular file in document root
RewriteRule .* - [QSA,L]
A dash indicates that no substitution should be performed, the existing path, .* , is passed through untouched
This rewriteRule is just after a rewriteCond, so rewriteCond apply only to this rewrite rule
The last but not the least : RewriteRule ^(.*)$ /index.php [QSA,L]
redirect all requests that don't match previous rewrite rule to file index.php
there are some flag in rewrite rule
Now, all your requests except real file are redirect to index.php
In your file index.php, you can add this code
<?php
var_dump($_SERVER);
var_dump($_GET);
It's time to test, if it's well configure, you can try some request like
http://example.com/somesite/somefolder
http://example.com/somesite/anotherfolder?param=value
You should see var_dump of $_SERVER and $_GET If you want to develop a simple router, you can use $_SERVER['REQUEST_URI]
It's just a very very very simple router for example
<?php
$rawRoute = explode('/',$_SERVER['REQUEST_URI']);
switch($rawRoute[1]){
case 'blog': echo "this is blog page";
break;
case 'forum': echo "this is forum page";
break;
default: echo "no route matches, redirect to 404";
break;
}
You can look to this repository https://github.com/dannyvankooten/PHP-Router for understand how to work a router, if you're more reckless, you can look at symfony routing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.