homepage Welcome to WebmasterWorld Guest from 54.196.168.78
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Serving pages without extension
Prasawet




msg:4644021
 11:19 pm on Feb 10, 2014 (gmt 0)

My Q. is about serving some kind of pages without extension. Now I have this common solution, pages like
xxx-xxx-xxx-video.html
is served by
videos.php
where the .htaccess make difference based on -video.html and so on.
RewriteRule ^(.*)-video.html$ videos.php?video=$1 [NC]


I need to serve
xxx-xxx-xxx
page without marker and extension and the other kind of pages will stay intact.

E.G.
RewriteRule ^categorie.html$ catlist.php
will stay the same, so just kind of pages above need to be served on the mentioned way.

Please propose some solution.

Options -Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
<Files .htaccess>
Order Deny,Allow
Deny from all
</Files>
AddType application/x-httpd-php .html
#
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
#
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC]
RewriteCond %{QUERY_STRING} (\;|\'|\"|\%22).*(union|insert|where|select|update|drop|md5|or|and|if).* [NC]
RewriteRule ^(.*)$ 404.html [NC]
RewriteRule (,|;|<|>|'|`) 404.html [NC]
#
RewriteRule ^(.*)-video.html$ videos.php?video=$1 [NC]
RewriteRule ^categorie.html$ catlist.php
----
#
ErrorDocument 404 /config.php?action=p404

 

lucy24




msg:4644063
 1:54 am on Feb 11, 2014 (gmt 0)

I need to serve
xxx-xxx-xxx
page

Eeuw. Get rid of all those .* as they are precisely what you don't want. If you've got a pattern, make a Regular Expression to fit it. Here you're showing
([^/.-]+-[^/.-]+-[^/.-]+)\.html

or possibly even
(\w+-\w+-\w+)\.html

Or did you mean
(\w+-\w+-\w+)-videos\.html
?

I don't understand what you mean by "without extension" since all your examples in fact have the .html extension.

Other stuff:
-- escape all literal periods in patterns
-- make sure every rule includes the [L] flag unless you have a clear and specific reason for omitting it
-- begin all targets with / if they are internal rewrites
-- the [NC] flag isn't needed unless you're matching specific text
-- what have you got in your query strings that would call for the [NE] flag?
-- leave a blank line after each RewriteRule for your own sanity
-- group your htaccess file by module. No reason to say "RewriteEngine on" anywhere other than immediately before your first RewriteRule.

RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC]
RewriteCond %{QUERY_STRING} (\;|\'|\"|\%22).*(union|insert|where|select|update|drop|md5|or|and|if).* [NC]
...
RewriteRule (,|;|<|>|'|`) 404.html [NC]

If all those characters needed to be escaped-- they don't-- they would also need escaping in the body of a rule. Anyway, what are the pipes for? Since nothing in the list is an anchor, they can be compressed to
[,;<>'`]
or, if bad requests are an ongoing problem, cover your bases with something more like
[^\w/.-]
Here the [NC] flag is obviously meaningless because non-alphabetics don't have case.

Anyway, why is all that complicated stuff about the query string expressed as a RewriteRule? Wouldn't it be simpler to let your php pages run a preliminary check? At an absolute minimum, constrain the rule to
\.php$
since it's not likely anything else will ever have a query string, let alone a bad one.

Prasawet




msg:4644172
 12:08 pm on Feb 11, 2014 (gmt 0)

Thank you for your advice, respect it very much.

I did some of suggested changes and maybe I should reverse my Q.

If anything come to my site formated like:
xxx, xxx-numbers, numbers-xxx, xxx-xxx-xxx
, so it could be letters & numbers combination or just letters, it should be served by videos.php.

But if something come with the specific word like
categorie
, that should be served by catlist.php.

In both cases the .html extension (or any) is not an option so it sholud be send to 404 (by .htaccess or .php).

Now I am doing with specific (-video.html or -categorie.html) to make a difference.

Options -Indexes
Options +FollowSymlinks
RewriteBase /
<Files .htaccess>
Order Deny,Allow
Deny from all
</Files>
AddType application/x-httpd-php .html
# Redirect www urls to non-www
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
#
RewriteRule ([,;<>'`]) 404.html [L]
#
RewriteRule ^(.*)-video.html$ videos.php?video=$1 [NC,L]
RewriteRule ^categorie.html$ catlist.php [NC,L]
#
ErrorDocument 404 /config.php?action=p404

Prasawet




msg:4644244
 3:40 pm on Feb 11, 2014 (gmt 0)

In the meantime, I use something similar you mentioned:
(\w+-\w+-\w+)\.html

with this correction:
RewriteRule ^([\w\-]+)$ videos.php?video=$1 [NC,L]

and got it what I want. PhP now serving everything without extension and if is not defined already.
If something come with .html (and is not treated already) it goes to 404.

My .htaccess now looks like this:
Options -Indexes
Options +FollowSymlinks
RewriteBase /
<Files .htaccess>
Order Deny,Allow
Deny from all
</Files>
AddType application/x-httpd-php .html
# Redirect www urls to non-www
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
#
RewriteRule ([,;<>'`]) 404.html [L]
#
RewriteRule ^([\w\-]+)$ videos.php?video=$1 [NC,L]
RewriteRule ^categorie.html$ catlist.php [NC,L]
RewriteRule ^choice.html$ choice.php [NC,L]
#
ErrorDocument 404 /config.php?action=p404

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved