Mod_rewrite - images, styles and scripts missing - .htaccess

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]

Related

Rewrite url path to new path using .htaccess

in my htaccess file, i have this, that is supposed to remove the index.php? from the path and make the path look like this: video/:AccountId/:recordId however something isnt working correctly. it doesnt re-write anymore and i can only make the url work by having the index.php? tag in it, so instead it looks like: video/index.php?/:AccountId/:recordId
<IfModule mod_rewrite.c>
RewriteEngine On
# !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
# slashes.
# If your page resides at
# http://www.example.com/mypage/test1
# then use
# RewriteBase /mypage/test1/
RewriteBase /video
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I cant just remove it because the clients already have the video/:AccountId/:recordId i dont want to have to email blast them to change something on their end to make this work, i was hoping too that maybe someone would know how to redirect that url to the one that includes the index.php? because overall i dont care that its there or not, its a hold over from previous teams.
With your shown samples, please try following htaccess rules file. Make sure your htaccess file is present along with video OR first level of URL folder, also make sure index.php is present inside that folder eg: `.htaccess and video are existing together AND index.php is inside video folder).
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/(.*)/?$ /$1/index.php?/$2/$3 [QSA,L]

redirecting with htaccess causes too many redirects

The website I'm working on is using some cms. I need to add a static website to this. When I put mypage.html in the main directory and go to www.website.com/mypage.html it works. I would like the page to be accessible without '.html' ending. I experimented with editing htaccess files but always end up with error of too many redirections.
What I entered were various combinations, for example
Redirect 301 http://website.com/mypage http://website.com/mypage.html
The htaccess file I'm using looks like this:
:Location /*.php
Use php54
:Location
RewriteEngine On
DirectoryIndex index_prod.php
Options -Indexes
RewriteRule ^.*\.(css|png|swf|js|gif|jpeg|jpg|flv|pdf|doc)$ - [L]
RewriteRule ^net2ftp - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^/?$ plug.html [L]
#RewriteCond %{REQUEST_URI} !=/
RewriteRule ^/?.* index_prod.php
I'm looking for tips or to be explicitly told what and where to put in htaccess file to make it work (if it's possible)
Could you please try following, considering that you want without extension file URLs to be served by html extension files. Also since you didn't mention any specific condition before RewriteRule hence that redirection errors are coming to it, because its keep on redirecting in lack of any condition/check's presence(till its maximum redirection limit is crossed).
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [NC,L]

.htaccess changes to convert query string to directory format

Basically I'm trying to convert my query string urls to a directory structure for SEO purposes. And I have been getting to the point where im trying to say the same logic in many semantical ways in the .htaccess file to see if I can get a result. Here is what I'm looking to do.
The request is:
mywebsite.com/test/antitrust/somelongfilename/
Then modified to request:
mywebsite.com/test/antitrust/?id=news&post=news/somelongfilename.html
So that when people click on my SEO friendly links they get changed to the ugly ones to which my php code can read. "somelongfilename.html" is not to be shown by itself, its just content thats pushed to a content area of a php file.
FYI, post=news/somelongfilename.html is actually the directory/file to goto from the antitrust directory, so its located /test/antitrust/news. Its been working fine even though its probably not a good idea to put / in a query string.
The .htaccess is in the subdirectory /antitrust, as I don't want to touch the root directory .htaccess as its servicing many websites that I dont want to affect since im not a .htaccess expert.
RewriteOptions inherit
RewriteEngine On
RewriteCond %{HTTP_HOST} news
RewriteRule ^/(.*) index.php/?id=news&post=$1 [QSA,L]
Also tried many variations of this:
RewriteEngine On
RewriteOptions inherit
RewriteCond %{HTTP_HOST} ^news$
RewriteRule \/(.+?)\/? ^?id=news&post=$2 [QSA,L]
RewriteRule ?id=(.+?)&post=(.+?) http://mywebsite.com/test/crush/antitrust/$1/$2/ [QSA,R]
Any ideas on what part im doing wrong? Thanks.
Try:
RewriteEngine On
RewriteOptions inherit
RewriteBase /test/antitrust/
# not sure why you need this, it's not part of your question
RewriteCond %{HTTP_HOST} news
# conditions to prevent looping
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?$ index.php?id=news&post=$1 [QSA,L]

Basic URL rewriting: How to have a css link, img src, etc, ignore it?

I have a website running at localhost/pm and the RewriteBase is correctly set to /pm/. There is a link tag in my document: <link ... href="themes/default/css/default.css">.
When the url is localhost/pm or localhost/pm/foo the CSS works all right. When there are more slashes in the URL, however, like localhost/pm/foo/bar the relative URL if the stylesheet changes to foo/themes/default/css/default.css.
How do I get this to work without having to put some sort of PHP path resolution in the link tag?
# invoke rewrite engine
RewriteEngine On
RewriteBase /pm/
# Protect application and system files from being viewed
RewriteRule ^(?:system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
EDIT:
Basically what I need now is this:
If request contains folder name /themes/ scrap everything that is before /themes/ and rewrite the rest to /pm/themes/...
I tried it like this: RewriteRule ^.*(/themes/.*)$ /pm/themes/$1 but I get an internal server error. Why?
If I do it like this: RewriteRule ^.*(/themes/.*)$ /pm/themes/ (ie. just remove $1 from the end) and use the URL http://localhost/pm/foo/themes/foo/ the resulting physical location is http://localhost/pm/themes which is what is expected too, which in turn means that at least my regex is correct. What am I missing?
The RewriteRule is almost correct
RewriteRule ^.*(/themes/.*)$ /pm/themes/$1
This rewrites http://localhost/pm/foo/themes/default/css/default.css to http://localhost/pm/themes/themes/default/css/default.css, which is one themes too much. Use this instead
RewriteRule /themes/(.*)$ /pm/themes/$1 [L]
But now you have an endless rewrite loop, because /pm/themes/.. is rewritten again and again. To prevent this, you need a RewriteCond excluding /pm/themes
RewriteCond %{REQUEST_URI} !^/pm/themes/
RewriteRule /themes/(.*)$ /pm/themes/$1 [L]
Now the request is rewritten only once and you're done.
You probably need to add the following lines before your RewriteRule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
It will only evaluate your rewrite rule if the requested file or directory doesn't exist.
You should post your .htaccess file so we can offer better advice

RewriteRule For Matching Arbitrary PHP Files

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]

Resources