I actually thought there should already be a thread about this but i wasn't able to find one.
let's say i have a website with this structure:
/index.php
/.htaccess
/template
/template/css
/template/css/foo.css
I'm redirecting everything to index.php with .htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ index.php?0=$1 [QSA,L]
Now I've got a problem with the css. that i could fix by placing another .htaccess in /template with this
RewriteEngine off
Options -Indexes
And then access the css file with http://example.com/template/css/foo.css but actually i would prefere to have just http://example.com/css/foo.css
So i tried to put in /.htaccess the following code (that didn't worked). What did i do wrong?
RewriteEngine on
RewriteBase /
RewriteRule ^/css/(.*) www/css/$1 [L]
RewriteRule ^/js/(.*) www/js/$1 [L]
RewriteRule ^(.*)$ index.php?0=$1 [QSA,L]
note I don't have any .htaccess in /template!
You should remove the htaccess file in the css folder. You just need to exclude rewrites to the template directory. You can do it all in a single file:
RewriteEngine on
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/template/%{REQUEST_URI} -f
RewriteRule ^(.*)$ /template/$1 [L]
RewriteCond ${REQUEST_URI} ^/template/
RewriteRule ^(.*)$ index.php?0=$1 [QSA,L]
The first rule handles requests for the css directory, and the second rule is the one you had that routes to index.php except if the request is for template.
Related
I would like to rewrite my URL only when typing this:
www.domain.com/dir1/dir2/dir3/
in that path there is a index.html file that will be automatically loaded. And I'd like it to become this:
www.domain.com/index.html
It is important that the root www.domain.com does not redirect to that subdirs because it already redirects to another .php file.
I wrote the following .htaccess file:
RewriteRule ^dir1/dir2/dir3/(.*)$ dir1/dir2/dir3/$1 [L]
but it redirects even when typing the root domain.com.
Could you help me with that?
Edit:
I have tried in another browser and the real problem is that when typing domain.com/dir1/dir2/dir3 or domain.com/dir1/dir2/dir3/index.html the dir1/dir2/dir3/ is still shown and is not hidden.
Edit 2:
my complete .htaccess:
AddHandler application/x-httpd-php56s .php
ErrorDocument 404 /404/404.html
ErrorDocument 500 /404/404.html
RewriteEngine on
RewriteBase /
Options -Indexes
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /404/404.html [R=404,NC,L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /404/404.html [R=500,NC,L]
Thanks
Replace your current code with this:
DirectorySlash On
RewriteEngine On
# skip all known resources from rewrite rules below
RewriteRule \.(?:jpe?g|gif|bmp|png|ico|tiff|css|js)$ - [L,NC]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+dir1/dir2/dir3/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^(?!dir1/dir2/dir3/)(.*)$ dir1/dir2/dir3/$1 [L,NC]
I have site e.g. 123.123.123.123/subdirectory/ and I would like to rewrite
123.123.123.123/subdirectory/page/123
to
123.123.123.123/subdirectory/page.php?id=123.
I've created .htaccess file:
RewriteEngine On
RewriteBase /subdirectory
RewriteRule ^page/(.*)$ page.php?id=$1 [NC,L,QSA]
but it doesn't work (rewriting in on).
Use this rule and it will work.
RewriteRule ^123\.123\.123\.123/subdirectory/page/123$ /123.123.123.123/subdirectory/page.php?id=123&%{QUERY_STRING}
I have a site like "abc.com/1_en-Application.html . Now i want to change it to abc.com/application.html.
I have .htaccess like
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([0-9]+)-([0-9]+)_(.*)-(.*).html$ index.php?id=$1&page=$2&lang=$3 [L]
RewriteRule ^([0-9]+)_(.*)-(.*).html$ index.php?id=$1&lang=$2 [L]
</IfModule>
Please help me how can i do this ?
Add the 3 lines below the existing rewrite rules:
RewriteCond %{REQUEST_URI} /application.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^application.html$ index.php?id=1&lang=en [L]
If you don't have a real file called application.html in the web root directory, this will rewrite the URL and internally direct it to index.php
If application.html is a real file, it will be shown instead. In that case, the 3 lines will still work but not necessary to include.
Here is my case. I'm trying to use .htaccess for rewrite url, but no matter how I try, browser always show 500 error or 404 error.
here is my code.
RewriteEngine On
RewriteBase /~torinots/beta/
RewriteCond %{REQUEST_FILENAME} !^(/beta/home)
RewriteRule ^home$ index.php [L]
example path: http://xx.xx.xx.xx/~username/beta/
Pls advice.
Update
I found this work!
RewriteEngine On
RewriteBase /~torinots
RewriteRule ^beta/home/?$ beta/index.php [L,NC]
I assume you have the .htaccess file in /~username/beta, since you use paths relative to that in the example above. Having a simple rule like the following rule will correctly internally rewrite the url, assuming that no other rules in .htaccess files higher up are interfering.
RewriteRule ^home$ index.php [L]
If you want to redirect a request to index.php too, then you need to prevent an infinite loop from happening. You can either use the END-flag (version 2.3.9 and up) or the THE_REQUEST trick.
#internal rewrite, and then stop everything
RewriteRule ^home$ index.php [END]
#Rewritebase is possibly needed for the redirect
RewriteBase /~torinots/beta/
#external redirect
RewriteRule ^index\.php$ home [R=301,L]
or:
#internal rewrite, and then stop everything
RewriteRule ^home$ index.php [L]
#Rewritebase is possibly needed for the redirect
RewriteBase /~torinots/beta/
#external redirect
RewriteCond %{THE_REQUEST} index\.php
RewriteRule ^index\.php$ home [R=301,L]
I have some folders in root of my server:
css/
js/
gfx/
new_www/
...
and some more. Assuming that I want in folder new_www copy of current page, but force it to use some things from root folder (for example gfx folder). I've tried following with .htaccess but its not working:
RewriteEngine on
RewriteBase /
RewriteRule /new_www/_js/(.*) /_js/$1 [L]
RewriteRule /new_www/gfx/(.*) /gfx/$1 [L]
What am I doing wrong? .htaccess is placed in root of folder.
Please understand that RewriteRule doesn't match leading slash in an URI when used in .htaccess. Your code should be replaced with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^new_www/(_js/.*)$ /$1 [L,NC]
RewriteRule ^new_www/(gfx/.*)$ /$1 [L,NC]
OR even better will be to have just one rule like this:
RewriteRule ^new_www/(.+)$ /$1 [L,NC]