I'm trying to write a rule for a directory of my site.
I tried something like this:
RewriteEngine on
RewriteRule ^(css|font|img|js)/([a-zA-Z0-9\/\.])$ $1/$2 [L]
RewriteRule ^([a-zA-Z0-9\/\_]*)$ index.php?url=$1 [L]
but I cant understand why it is not working well.
1- I want users have direct access to these directories css|font|img|js
(just for files, not directory lists. rewrite directory list to 403.php)
2- I want users have direct access to *.js|*.css files in view directory.
(not other file. rewrite other to 403.php)
3- I want to rewrite every other urls to index.php?url=$1
thanks in advance.
This part of your pattern: /([a-zA-Z0-9\/\.])$ means a slash, followed by exactly one character of a letter, number slash or a dot, then the end. I'm guessing you want something like:
ErrorDocument 403 /403.php
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(css|font|img|js)/ - [L,F]
RewriteCond %{REQUEST_URI} !\.(js|css)$
RewriteRule ^view/ - [L,F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?url=$1 [L,QSA]
You can try these rules:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !^(css|font|img|js)/ /403.php [NC,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} \.(css|js)$ [NC]
RewriteRule !^view/ /403.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [L,QSA]
How about using a RewriteCond?
You could do something like:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(css|font|img|js)/
RewriteRule .* - [L]
RewriteRule ^([a-zA-Z0-9\/\_]*)$ index.php?url=$1 [L]
This way you do a noop rewrite for files inside the css, font, img and js directories and do the index.php rewrite for all other URLs.
RewriteEngine on
Options -Indexes
RewriteCond %{REQUEST_URI} .php$ [OR]
RewriteRule ^(.*)$ target_file.php?=url$1 [L,NC]
#allow access to file js and css
RewriteCond %{REQUEST_URI} .(css|js)$
RewriteRule ^(.*)$ - [L,NC]
When use [L, NC] don't redirect to other page:
If you access localhost/page.php this don't redirect to target_file.php?url=/page.php
but continue in same url and appears the file target_file.php
Related
I'm trying to shorten my URL but sadly can't find anything that helps.
I divide my code in folders. Index is positioned at root just like my .htaccess.
The folders are named like the file extensions, so php, js, css [...]
I have a link like the following:
localhost/php/getBets.php
and want it to be
localhost/getBets/
I already have the part that cut's the .php extension at the end, so here is my full .htacces
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Hide Index
IndexIgnore *
# Forbid accessing certain sites
RedirectMatch 403 ^/.gitignore$
RedirectMatch 403 ^/.htaccess$
RewriteRule ^(?!index)(?!.*getBets).*\.(php|rb|py|txt|md|sql|inc)$ - [F,L,NC]
# Hide .php file ending in URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Can someone maybe tell me how I could achieve this? :)
Thanks alot!
For your required url you can use below rule in root directory it is for rewriting,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/$ php/$1.php [L]
Actualy Apache still does not have pathinfo($,PATHINFO_DIRNAME), function like has PHP.
Use %{REQUEST_URI}, like this example:
RewriteRule ^(.+)/$ /path-dirname/$1 [R=301,L]
may reset with:
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^.+/$ %1 [R=301,L]
I have created my first Php solution but have trouble removing the .php extension from the URL. It works in the root but not in sub-folders?
Eg www.domain.com/index.php reached from www.domain.com/index (or www.domain.com/)
Then it gets tricky.
For example. www.domain.com/en/europe/bikes/racer-bike.php will not differ from www.domain.com/en/bikes/racer-bike without ending up on www.domain.com/en/bikes/ and on index.php
My .htaccess file looks like this:
Options -MultiViews
RewriteEngine on
RewriteBase /
# Remove .php-extension from url
RewriteCond %{REQUEST_URI} ^/(.*).php$
RewriteRule ^(.*)$ %1 [L,QSA]
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# SSL
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^/?(.*)$ https://%{SERVER_NAME}/$1 [L,R=301,QSA]
I Try it and it works:
My dirs structure
/
index.php
/sub
file.php
And my htaccess like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^\/]*)$
RewriteRule ^([^\/]*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ([A-Za-z]+)/(.*)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^([A-Za-z]+)/(.*)$ $1/$2.php [NC,L,QSA]
Try it
You are doing it wrong ... Mostly you do not make php files for each uri (route). You should not remove php extension you should direct all uri's to index.php and use some router to define how to handle each request. or you can just use some framework to do that like laravel or symfony etc.
I have problem with my rewrite rule. I have new webpage in root/hrp. and if i open pages in that directory, then all is OK, but when i wanna surf those pages from root (without /hrp/) then is problem. I found some .htaccess rules and all working fine, but if i wanna execute some pdf,jpg,or php script directly, then come problem.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} !hrp/
RewriteRule (.*) /hrp/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/hrp/index.php
RewriteRule ^(.*)$ /index.php [L]
if i put this bottom code on begining, i can open files directli, but pages don't work and vice versa
RewriteCond %{REQUEST_FILENAME} (/|\.php|\.html|\.htm|\.png|\.jpg|\.jpeg|\.gif|\.xml|\.rss|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule ^(.+)$ $1 [L]
Try adding this at the top instead:
RewriteRule \.(php|html?|png|jpe?g|gif|xml|rss|feed|pdf|raw)$ - [L,NC]
or you can try adding this condition to your hrp rule:
RewriteCond %{DOCUMENT_ROOT}/hrp/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/hrp/$1 -d
RewriteRule (.*) /hrp/$1 [L]
I have find solution
don't rewrite this files
RewriteCond %{REQUEST_URI} !.(gif|jpg|png|ico|css|js|swf|wav|mp3|less|cur|pdf|jpeg|txt|ico|zip|gz) [NC]
don't rewrite this folder - there i have php files, and dont rewrite this files
RewriteCond %{REQUEST_URI} !(folder1|folder2|file1.php|file2.php|file3.php) [NC]
all content is in "/hrp/" folder
RewriteCond %{REQUEST_URI} !hrp/
RewriteRule (.*) /hrp/$1 [L]
but url don't contains "/hrp/"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/hrp/index.php
RewriteRule (.*) /index.php [L]
I would like to have a RewriteRule match the "base" url/path of my site.
My base URL, once in prod, will be something like http://www.site.com, but is currently http://localhost/dev/site/ in dev. .htaccess file is in the "site" folder.
In the following examples, the first RewriteRule is the problem, and I can't get it to work. The 2nd and 3rd RewriteRules work fine. I need it to bring to index.php?l=default&rte=index if I reach the base path/URL.
Thanks.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/$ index.php?l=default&rte=index [NC,L]
RewriteRule ^([a-zA-Z]{2})/$ index.php?l=$1&rte=index [NC,L]
RewriteRule ^([a-zA-Z]{2})/([a-zA-Z-]+)$ index.php?l=$1&rte=$2 [NC,L,QSA]
Try these rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^/?$ index.php?l=default&rte=index [QSA,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([a-zA-Z]{2})/?$ index.php?l=$1&rte=index [QSA,L]
RewriteRule ^([a-zA-Z]{2})/([a-zA-Z-]+)/?$ index.php?l=$1&rte=$2 [QSA,L,QSA]
I have a htaccess file that I am try to make a rewrite but it causes me multiple problems. First here is my htacess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]
RewriteRule ^([a-zA-Z0-9]*)/?$ index.php?category=$1&%{QUERY_STRING}
RewriteRule ^([a-zA-Z0-9]*)/([a-zA-Z0-9]+)/?$ index.php?category=$1&itemId=$2&%{QUERY_STRING}
RewriteRule ^login$ login.php?category=$1&%{QUERY_STRING} [L]
The first problem is that when I make a request like "mysite/foo/bar/", it does rewrite the query string in category and itemId but it also changes the base directory for my css files and Js files to /foo/bar/design/css/style.css
The second problem is that I have a few path like "login/","register/","users/", and when I try to load the corresponding file, it says:
The requested URL /login/ was not found on this server.
Thanks for the help!
First change your .htaccess like this:
RewriteEngine On
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## don't do anything
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]
RewriteRule ^([a-zA-Z0-9]*)/?$ index.php?category=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9]*)/([a-zA-Z0-9]+)/?$ index.php?category=$1&itemId=$2 [L,QSA]
RewriteRule ^login/?$ login.php?category=$1&%{QUERY_STRING} [L,QSA]