Rewrite rules for custom CMS - php

I'm in the process of building a custom CMS for a client and I'm getting stuck on rewrite rules in .htaccess. I'm trying to rewrite anything to page/?pid=xxx -- so, for example, /about becomes page/pid=123.
I assume the problem that I'm having is twofold: I'm building this site in a subdirectory for the time being (/clients/afsb) and I've also got an existing rewrite rule in place to remove the .php from file extensions. Another possible wrinkle is that I'd like to be able to have the rewrite rule ignore any files that actually exist -- for example, the home page is at index.php (or just /, in this case) and I wouldn't want the rule to affect that. Likewise I've got two other pages that are not dynamic (/contact, for example.) Here's what I've tried in .htaccess for the time being:
<Files php.ini>
order allow,deny
deny from all
</Files>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /clients/afsb
# Redirect to remove .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# Redirect to "page" for dynamic pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ page?&pid=$1
<Files ~ "\.inc$">
Order allow,deny
Deny from all
</Files>
I'm obviously not too strong with .htaccess or regex in general so I'd appreciate any direction you can give. Thanks!

Figured it out, this is what I needed -- I was missing the caret. Thanks to both for looking into it.
# Redirect to remove .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# Redirect to "page" for dynamic pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ page?pid=$1 [L]

Related

ignore folder from htaccess rewriterule

I don't understand htaccess at all except from it's function to "pretify" url's and I know this question has been asked a milion times, i have tried nearly all of them but none of the answers worked for me.
So basically i have givin my own go at creating a cms and a structure to go with it. the "cms" is located in a subfolder called system with it's own respectable index.php
my htaccess is configured to take the var "page" and retract the .php extension from it in the url. everything works fine until i start calling pages from the system subfolder.
My question is, how can i get htacces to ignore this folder and continue it's rewriting rule despite the user currently being located in the subfolder?
my htaccess is set up as follows:
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\\.]+)$ $1.php [NC,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [QSA]
<Files .htaccess>
order allow,deny
deny from all
</Files>
ErrorDocument 404 home
ErrorDocument 403 home
#php_flag display_errors off
Have it like this:
ErrorDocument 404 /home
ErrorDocument 403 /home
RewriteEngine On
Options +FollowSymLinks
# skip subfolder from any rules
RewriteRule ^subfolder(/.*)?$ - [L,NC]
# add .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^system/(.*)$ system/index.php?page=$1 [L,NC,QSA]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
<Files .htaccess>
order allow,deny
deny from all
</Files>

Can't overwrite smarty block in OXID eShop

I am making new plugin for Oxid E-shop. In my metadata file, description, title, logo, works perfectly and I can see plugin in backend, but when I activate plugin, nothing in frontend is changed. Blocks aren't overwritten.
/modules/myModule/metadata.php
'blocks' => array(
array(
'template' => 'layout/footer.tpl',
'block' => 'footer_main',
'file' => '/views/blocks/layout/footer.tpl'
)
I have just simple example in footer for now.
/modules/myModule/views/blocks/layout/footer.tpl
[{block name="footer_main"}]
<div class="footer-base">Schuberth test !</div>
[{/block}]
Location of template that should be overwritten: /application/views/azure/tpl/layout/footer.tpl
it should actually work, at least with versions >= 4.8
Check if your block entry was saved in database table oxtplblocks.
Sometimes OXID does not update cached metadata after you added new entries to metadata.php, in this case try to increase module version.
Also check your file names for typos.
If everything is there and correct, you should post the full code and tell us your shop version.
Opening oxtplblocks table in database discovered me a bug, OXMODULE column has lenght 32 characters, and my module ID/directory name was longer, so rest of name was cut off.
I found on other source that module id shouldn't be longer than 20 characters.
Sometimes it happend that you need to clear the cache (tmp). this works for me most the time.
Please add .htaccess file to /source directory and copy this code.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^graphql/?$ widget.php?cl=graphql&skipSession=1 [QSA,NC,L]
RewriteCond %{REQUEST_URI} config\.inc\.php [NC]
RewriteRule ^config\.inc\.php index\.php [R=301,L]
RewriteCond %{REQUEST_URI} setup [NC]
RewriteRule ^setup(.*)$ Setup$1 [R=301,L]
RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
RewriteRule .* - [F]
RewriteCond %{REQUEST_URI} oxseo\.php$
RewriteCond %{QUERY_STRING} mod_rewrite_module_is=off
RewriteRule oxseo\.php$ oxseo.php?mod_rewrite_module_is=on [L]
RewriteCond %{REQUEST_URI} !(\/admin\/|\/Core\/|\/Application\/|\/export\/|\/modules\/|\/out\/|\/Setup\/|\/tmp\/|\/views\/)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !(\.html|\/|\.jpe?g|\.css|\.pdf|\.doc|\.gif|\.png|\.js|\.htc|\.svg)$ %{REQUEST_URI}/ [NC,R=301,L]
RewriteCond %{REQUEST_URI} !(\/admin\/|\/Core\/|\/Application\/|\/export\/|\/modules\/|\/out\/|\/Setup\/|\/tmp\/|\/views\/)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (\.html|\/)$ oxseo.php
RewriteCond %{REQUEST_URI} (\/out\/pictures\/generated\/)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (\.jpe?g|\.gif|\.png|\.svg)$ getimg.php [NC]
RewriteRule ^(vendor/) - [F,L,NC]
RewriteRule ^migration - [R=403,L]
</IfModule>
# disabling log file access from outside
<FilesMatch "(EXCEPTION_LOG\.txt|\.log|\.tpl|pkg\.rev|\.ini|pkg\.info|\.pem|composer\.json|composer\.lock|test_config\.yml)$">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order allow,deny
Deny from all
</IfModule>
</FilesMatch>
# Prevent .ht* files from being sent to outside requests
<Files ~ "^\.ht">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order allow,deny
Deny from all
</IfModule>
</Files>
Options -Indexes
DirectoryIndex index.php index.html
It should be good.
I was using Oxid v6.2. And it worked for v6.4 as well.

remove and disallow extensions with .htaccess

I found this script or removing extensions from php files:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
While this works, it still allows files to be accessed if you type them in with their extension, which I don't want.
I tried the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([.]+)$ $1.php [NC,L]
I also tried ^(.*)$ and ^(.+)$
which seems like it should do the job, because it would do this:
index.php -> index.php.php
but somehow, it doesn't work as expected.
So how do I update the above .htaccess script to disallow file extensions?
EDIT:
My .htaccess script seems to be detecting the files correctly:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule "^.*\.php$" "other.html" [L]
RewriteRule "^([^\.]+)$" "other2.html" [L]
The pages other.html and other2.html simply contain the words "OTHER" and "OTHER 2"
Now, using the above script, the output is as expected:
"/test.php" gives output "OTHER"
"/test" gives "OTHER 2"
but if I update the script to the following, both url variations start returning "OTHER"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule "^.*\.php$" "other.html" [L]
RewriteRule "^([^\.]+)$" "$1.php" [L] // changed
So it seems that after the extensionless filename has ".php" added to it by rule#2, it somehow gets caught by rule #1.
Aren't these rules ordered? and isn't [L] supposed to stop processing on a match?
EDIT 2:
So assuming [L] did what I was expecting... the following script would work...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule "^.*\.php$" "404.html" [L]
RewriteRule "^([^\.]+)$" "$1.php" [L]
Try this solution How to remove file extension from website address?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
edit: you can do a filematch to prevent executing all .php except index.php. I typically route most of my pages through index.php anyways so this solution would work if you only have a handful of base index files (index1.php,index2.php,etc). I'm sure you can come up with something from this if you don't find a working solution.
<FilesMatch "\.php$">
Order Allow,Deny
Deny from all
</FilesMatch>
<FilesMatch "index[0-9]?\.php$">
Order Allow,Deny
Allow from all
</FilesMatch>
This works:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule "^.*\.php$" "404.html" [END]
RewriteRule "^([^\.]+)$" "$1.php" [END]

Enter own .htaccess rewrite rules in wordpress

I want to write a own rewrite rule into my htacces. The redirection itself works fine but Wordpress validates the URI with $_SERVER["REQUIRE_URI"]. So the site Title is Changed automatically to page not found, the loop doesn't works and so on.
Is there anyway to do this?
If not is there a other possibility actually I only need a really simple redirect from "www.example.com/test/" to "www.example.com/t.php?a=1" So it's completely static i don't need patterns at all. I have already tried it with "frameset" but that looks pretty ugly and makes the process slow.
Thank you very much
Jakob
My .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# My Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^Malta/$ http://www.reisekontrast.de/Index?malta=1 [QSA,L]
RewriteRule ^China/$ http://www.reisekontrast.de/Index?china=1 [QSA,L]
# WP Rules
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
We need your original .htaccess file. This is an easy rewrite (redirect), but wordpress already has URL Rewriting rules. To help you best would mean we need to work cohesively with what settings you already have running. Otherwise your are just going to break Wordpress but get your URL's working.
Also, as a primer question, does your server support full override in htaccess?
Based on your HTACCESS file, I would add the section for "Custom" Rules. That way it bypasses the Rewrite Conditions that are arbitrary in this type of rewrite.
Hope this helps! Let me know if I can provide more detail or other options.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Custom rules
RewriteRule /test/(.*) http://example.com/t.php?a=1 [NC]
# My Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^Malta/$ http://www.reisekontrast.de/Index?malta=1 [QSA,L]
RewriteRule ^China/$ http://www.reisekontrast.de/Index?china=1 [QSA,L]
# WP Rules
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

how to make phpmyadmin work with kohana site? (.htaccess)

today i was trying to upload phpmyadmin to one of the folders with kohana site, but it turned out that instead of phpmyadmin i get empty page. so i think that this is the problem of rewriting rules in .htaccess in the root directory of the site.
.htaccess file has the following lines:
RewriteEngine On
RewriteBase /
<Files .*>
Order Deny,Allow
Deny From All
</Files>
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
my phpmyadmin index.php file is located in phpmyadmin/ directory, but, as i've understood, every request is redirected to index.php in the root directory. how to make phpmyadmin work not breaking routing of kohana?
thanx for help!
Just add another RewriteCond before the last RewriteRule:
RewriteCond %{REQUEST_URI} !^/phpmyadmin
or to be more explicit, you can add another rule before the existing RewriteConds:
RewriteCond %{REQUEST_URI} ^/phpmyadmin
RewriteRule ^.*$ - [NC,L]

Categories