I have two rules in my htaccess file and while the first two are working I can't figure out why the third one isn't.
What I have currently is
Options -Indexes
Options +FollowSymLinks
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?key=$1&id=$2
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ index.php?key=$1&id=$2
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?key=$1&id=$2&user=$3
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ index.php?key=$1&id=$2&user=$3
<files config.php>
order allow,deny
deny from all
</files>
<files loader.php>
order allow,deny
deny from all
</files>
I can go to localhost/page/5 and I can go to localhost/page just fine, but if I try to go to localhost/module/5/1 it breaks and throws me a 404 error.
Any help on this would be greatly appreciated as I don't see anything, so it could be a simple yet stupid typo on my part.
None of your rule regex are matching /module/5/1 URI. Have your rules like this:
RewriteEngine On
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(\w+)/?$ index.php?key=$1 [L,QSA]
RewriteRule ^(\w+)/(\w+)/?$ index.php?key=$1&id=$2
RewriteRule ^(\w+)/(\w+)/(\w+)/?$ index.php?key=$1&id=$2&user=$3 [L,QSA]
\w matches [a-zA-Z0-9_]
Related
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>
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.
I'm trying to rewrite example.com/news.php to example.com/news or example.com/news/. With my code example.com/news works just fine but example.com/news/ gives me a 404 Not Found.
My code:
<Directory /var/www/>
Options -Indexes SymLinksIfOwnerMatch -MultiViews
AllowOverride None
Order allow,deny
allow from all
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^games/csgo/([0-9]+)$ /games/csgo/view.php?id=$1 [L]
</IfModule>
</Directory>
How can I redirect example.com/news.php to example.com/news AND example.com/news/?
You should better stick to one url type! (with or without trailing slash) and redirect the wrong type to the other,
this code is for removing slash from the end of url :
RewriteRule ^(.*)/$ /$1 [L,R=301]
If you add RewriteCond %{REQUEST_FILENAME} !-d and RewriteCond %{REQUEST_FILENAME}.php -f before that it works also with sub directories.
An obsolete Wordpress image plugin has left me with over 4000 404 errors.
To solve the problem, I'd like to batch 301 redirect thousands of old attachment link URLs to point towards the original source image files:
The old URLs that give a 404 error look like this:
http://www.hongkonghustle.com/?pagename=album&?pp_album=main&pp_cat=default&pp_image=zombie_boy_tattoo_lady_gaga_rick_genest.jpg
I'd like to redirect them each to the wp-content/photos directory where the file actually exists:
http://www.hongkonghustle.com/wp-content/photos/zombie_boy_tattoo_lady_gaga_rick_genest.jpg
From what I've read, I believe I should alter my htaccess file and add a RewriteCond using the {QUERY_STRING} but I don't know exactly how.
Also, where should I put the changes relative to my current htaccess?
My current htaccess file includes the following:
Options +Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^hongkonghustle\.com
RewriteRule ^(.*)$ http://www.hongkonghustle.com/$1 [R=permanent,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.hongkonghustle.com/$1/ [L,R=301]
RewriteEngine On
RewriteBase /
<Files wp-config.php>
Deny from all
</Files>
<Files wp-config.php>
Deny from all
</Files>
Options +Indexes
IndexOptions -FancyIndexing
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Thanks again Anubhava! You're getting very close!
Now it goes to the /wp-content/photos/ directory, but the URL is still incorrect:
hongkonghustle.com/wp-content/photos/&/?pagename=album&%253fpp_album=main&pp_cat=default&pp_image=zombie_boy_tattoo_lady_gaga_rick_genest.jpg
We need the final URL to be:
hongkonghustle.com/wp-content/photos/zombie_boy_tattoo_lady_gaga_rick_genest.jpg
So, hongkonghustle.com/wp-content/photos/IMAGE_FILENAME.JPG
I think it's almost solved! Please update me if you have any ideas on how to accomplish this. Sincere thanks!
I ended up doing a 301 redirect like this in htaccess:
Options +Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^pagename=album&\??pp_album=main&pp_cat=default&pp_image=(.*)$
RewriteRule .* /wp-content/photos/%1? [L,R=301]
RewriteCond %{HTTP_HOST} ^hongkonghustle\.com
RewriteRule ^(.*)$ http://www.hongkonghustle.com/$1 [R=permanent,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.hongkonghustle.com/$1/ [L,R=301]
RewriteEngine On
RewriteBase /
<Files wp-config.php>
Deny from all
</Files>
<Files wp-config.php>
Deny from all
</Files>
Options +Indexes
IndexOptions -FancyIndexing
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Change your code to this:
Options +Indexes +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)pp_image=([^&]+) [NC]
RewriteRule ^$ /wp-content/photos/%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^hongkonghustle\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ %{REQUEST_URI}/ [L,R=301]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
<Files wp-config.php>
Deny from all
</Files>
IndexOptions -FancyIndexing
I've got the following .htaccess working perfectly:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.mysite.com/mypage.php?u=$1 [NC]
This will allow me to write urls like this:
http://www.mysite.com/peter => http://www.mysite.com/mypage.php?u=peter
But what if I want have an aditional rule to avoid writing the .php extension for existing files?
http://www.mysite.com/contact (contact.php exists on file)
I tried adding this to my curr htaccess (but doesnt work):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
In other words, I would like to have vanity urls and ignore the .php extension in the same .htaccess.
SOLVED!
Thanks to Ulrich Palha and Mario (didnt know about the ordering and the LAST [L] flag!
RewriteEngine on
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.mysite.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.mysite.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.mysite.com/mypage.php?u=$1 [NC]
It's a matter of ordering. RewriteRules are applied in the order they are noted in the .htaccess
See Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask?
Just add your new block as first rewrite statement, add a [LAST] flag, and it should work.
This rule works unconditionally (not guarded by any RewriteCond), so should always be the last of all RewriteRules:
RewriteRule ^(.*)$ ...
(And all preceeding RewriteRules should preferrably carry a [L] flag. You have a bit of a workaround there.)
For me this code work:
in "/etc/apache2/sites-available/default" or "httpd.conf"
you must have (at least) these options in the default directory
...
<Directory />
...
Options FollowSymLinks
AllowOverride All
...
</Directory>
...
OR in the site specific site directory
...
<Directory "/var/www/my-site">
...
Options FollowSymLinks
AllowOverride All
...
</Directory>
...
and create or edit the ".htacces" in the directory of the site (for example "/var/www/my-site/.htaccess") and add this code:
AddType application/x-httpd-php htm html php
AddHandler application/x-httpd-php .htm .html
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]