I'm having trouble rewriting a series of files in my directory with RewriteRules in .htaccess.
I have index.php?page=page_name
This one is working so far, and I've been successful with it.
#RewriteRule ^([A-Za-a0-9-]+)/?$ index.php?page=$1 [NC]
#RewriteRule ^([A-Za-a0-9-]+)/([0-9-]+)/([A-Za-a0-9-]+)?$ index.php?page=$1&id=$2&name=$3 [NC]
I've been able to do this for views, linked through the index.php file, which is the above named ones.
My challenge is linking files in public_html/view/css/style.css directory to make it look like public_html/css/style.css
This is what I've been trying so far, which isn't working:
#RewriteRule ^css/(.*)$ /view/css/$1 [NC,L]
I have files in:
view/css
view/js
and some other directory. I want them to look like:
domain_name.com/css/style.css
domain_name.com/js/style.css
Please tell me what am I doing wrong and why this is not working.
You can use this rule in your root .htaccess:
RewriteRule ^(js|css)/(.+)$ /view/$1/$2 [NC,L]
Remove the # sign; it turns the entire line into a comment and means your rule is never executed:
RewriteRule ^css/(.*)$ /view/css/$1 [NC,L]
RewriteRule ^js/(.*)$ /view/js/$1 [NC,L]
Related
On the start i want you to take notice, i did try finding solution in google, or in stackoverflow. I found some tips, but they didn't work for me, or i simply failed following instructions.
So, here is my site directories:
www.domain.pl/index.php <--index file
www.domain.pl/images/ <--images folder
www.domain.pl/styles/ <--styles folder, for now just 1 css file
www.domain.pl/script/ <--scripts folder, js files, and 1 php file called with include
www.domain.pl/font/ <--font files
Now, when i open my website with www.domain.pl or www.domain.pl/index.php it works like a charm. But i want to be able to add some parameter for example www.domain.pl/index.php?action=dosmth, but it doesn't look good for a users, so thats the place where I wanted to use mod rewrite.
Now comes tricky part, i want it to look like www.domain.pl/index/actionparam/ but when i do that, I get error in console, saying my images, scripts, and css cant be loaded/found. (i tried rewriting it like that: RewriteRule ^index/([^-]+)/script/jquery-1.7.1.min.js$ script/jquery-1.7.1.min.js [L] but I cant use it for every single image, script, css, or font file. There need to be better way.
So, my mod rewrite so far:
Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.pl/$1/ [L,R=301]
RewriteRule ^index.html$ index.php [L]
RewriteRule ^index/$ index.php [L]
RewriteRule ^index/([^-]+)/$ index.php?action=$1 [L]
First part, i am trying to add "slash" at the end of url, if user didnt put it there.
http://domain.pl/index->http://domain.pl/index/ or
http://domain.pl/index/myaction->http://domain.pl/index/myaction/
Question
Is it possible to somehow skip folders with images, scripts etc when rewriting it? Or how could i rewrite all images at once?
Additional question
http://domain.pl/myaction/ redirect straight to http://domain.pl/index.php?action=myaction right away, without need of putting /index/ in there? Yes, i am learning mod rewrite.
You don't have to include index in the request, only the parameter. Example:
Request: http://domain.pl/myaction
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# Exclude existing files and directories.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
# Get the parameter and pass it to index.php
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^([^/]+)/? /index.php?action=$1 [L,NC]
I'm trying to put together a rewrite condition in to match the last section of a URL that ends in 10 alphnumeric characters like:
http://www.someurl.com/EHN4K5LFWQ
and route it to:
http://www.someurl.com/index.php/EHN4K5LFWQ
I've put together the following so far but I can't seem to get it to work after struggling for a couple days using a number of variations both with and without a rewrite rule.
RewriteCond %{REQUEST_URI} ^dir1/dir2/([A-Z,0-9]{10})$
RewriteRule ^(.*)$ dir1/dir2/index.php/$1 [L]
Any help would be greatly appreciated.
If the htaccess file is in your document root, you can simply use this rule:
RewriteRule ^dir1/dir2/([A-Z,0-9]{10})$ /dir1/dir2/index.php/$1 [L]
If the htaccess file is in the dir2 folder, then you can use this rule:
RewriteRule ^([A-Z,0-9]{10})$ index.php/$1 [L]
I'm somewhat new to htaccess rewrite rules, and have been scratching my head for the past few days on what's happening here. No amount of Googling seemed to help, so hopefully somebody knows the answer.
I have a site that can be accessed as:
www.site.com
www.site.com/684
www.site.com/684/some-slug-name-here
All of these scenarios should go to index.php and pass in the optional id=684 and slug=some-slug-name-here
Which works fine.
My problem is I have a separate file. Right now it's called admintagger.php - but this fails when I call it anything. 21g12fjhg2349yf234f.php has the same issue.
The problem is that that I would like to be able to access admintagger.php from www.site.com/admintagger
but it seems to be matching my rule for index, and taking me there instead.
Here is my code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^imagetagger$ /imagetagger.php [NC,QSA]
RewriteRule ^([0-9]+)/?(.*)?/?$ index.php?id=$1&slug=$2 [NC,L,QSA]
If you want to arbitrarily be able to access php files via the name (sans extension) then you need to create a general rule for it. But you need to be careful otherwise you may be rewriting legitimate requests for existing resources (like a directory, or a slug). Try this instead:
# make sure we aren't clobbering legit requests:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# see if appending a ".php" to the end of the request will map to an existing file
RewriteCond %{REQUEST_FILENAME}.php -f
# internally rewrite to include the .php
RewriteRule ^(.*)$ /$1.php [L]
Then you can have your routing to index.php right after that:
RewriteRule ^([0-9]+)/?(.*)?/?$ index.php?id=$1&slug=$2 [NC,L,QSA]
Although you may be better off create a separate rule for each of your 3 cases:
RewriteRule ^([0-9]+)/([^/]+)/?$ /index.php?id=$1&slug=$2 [NC,L,QSA]
RewriteRule ^([0-9]+)/?$ /index.php?id=$1 [NC,L,QSA]
RewriteRule ^$ /index.php [L]
So I'm building an ecommerce website using a file structure suggested to me, it looks like this:
http://www.sample.com/index.php
http://www.sample.com/department/index.php
http://www.sample.com/department/men/index.php
Basically I want to get rid of all the index.php filenames in the url, so it looks like this:
http://www.sample.com
http://www.sample.com/department
http://www.sample.com/department/men
I've achieved this for my homepage with adding this code in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
But, is there any way to apply this rule to every subdirectory in my site with just editing the root .htaccess file? I'm guessing it can be done by adding this rule in .htaccess files in every directory but I'd rather just have one.
Also, is this even a good way to structure a website? I am not particularly fond of it as there are so many index.php files but I'm not sure of a better alternative...
Try putting these rules above the rules that you have in your htaccess file in the document root:
RewriteRule ^index.php$ / [L,R=301]
RewriteRule ^(.+)/index.php$ /$1/ [L,R=301]
I have entries in my .htaccess that goes like this:
RewriteRule ^$ dubai/ [R]
RewriteRule ^dubai/$ page/index.php [L]
RewriteRule ^$ pakistan/ [R]
RewriteRule ^pakistan/$ page/index.php [L]
basically I want to make a rule that can handle countries as part of the rewrite, the above code i have is redundant and can cause the file to grow unnecessarily.
Is there a way to automatically redirect the page to page/index.php if it serves for any countries? I tried putting this line to the last line above:
RewriteRule ^([a-zA-Z\/]*)/$ page/index.php
My problem is it no longer reads the succeeding rules for the other pages to serve. The below lines are samples of the rules just below the last one above:
RewriteRule ^([a-zA-Z\/]*)rent/$ page/index.php?methodcall=rent&for=Rent
RewriteRule ^([a-zA-Z\/]*)landlords/$ page/index.php?methodcall=landlords
What happens is the get vars are no longer read by index.php file, if i remove the last rewriterule the key vars are read properly.
Any suggestions?
Thanks in advance!
Try this:
RewriteEngine on
RewriteRule ^([a-z]+)(\/?)$ page/index.php [NC,QSA,L]
RewriteRule ^([a-z]+)/rent(\/?)$ page/index.php?methodcall=rent&for=Rent [NC,QSA,L]
RewriteRule ^([a-z]+)/landlords(\/?)$ page/index.php?methodcall=landlords [NC,QSA,L]