Rewrite url path to new path using .htaccess - .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]

Related

htaccess - skip URL past root level

Been trying all sorts of ways can't seem to figure out how to get the following result. I want my url to grab everything up to a specific point, link through that file and skip rest of url. The rest of the url is however necessary for my include files so I want it to remain in the URL.
This is the result I'm after:
www.homepage.com/index/somepage, This would open "index.php" and do nothing else.
www.homepage.com/stuff/someotherpage, This would open "stuff.php" and do nothing else.
So it's quite simple but I just can't figure out how to make this happen when using multiple pages. I can make it simply redirect to index.php every time, but that's not the result I want.
Progress so far:
<IfModule mod_rewrite.c>
RewriteEngine On
# Force everything through the index.php file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
You can start this way, capturing the first part of the URL and redirect to this page .php
You can change the ([a-z]+) if you have more complex page name.
<IfModule mod_rewrite.c>
RewriteEngine On
# Force everything through the index.php file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]+)/(.*) $1.php?param_url=$2 [L]
</IfModule>

Mod_rewrite - images, styles and scripts missing

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]

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

mod rewrite doing something weird

http://localhost/clean_urls/user/whtffgh redirect fine to this http://localhost/clean_urls/user/whtffgh/ (pretty much anything I put there works).
http://localhost/clean_urls/user/cohen however, redirects to this
http://localhost/clean/ and I have no idea why. It should just add a /
Heres my .htaccess code
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /clean_urls/user/$1/ [L,R=301]
RewriteBase /clean_urls/user/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?user=$1
</ifModule>
UPDATE:
The code works besides when I put cohen as the username. The second part of the .htaccess file is making user/index.php?user=username look like user/username so its just a $_GET variable that looks nice in the url and is SEO friendly (apparently).
I am just learning this stuff and making a webapp to try it out so not really sure where its going but I do think I will need another variable on the end. http:://localhost/clean_urls/user/fred/someverb
You misunderstand what the RewriteBase directive is used for. It helps the rewrite engine in a "per directory" context to convert the relative URIs to the correct filename equivalent.
You should only have one RewriteBase directive per .htaccess file. The rewrite engine by default will use the lowest .htaccess file on the path with the RewriteEngine On directive set.
So in DOCroot (that is the directory where a "GET /xxx" is mapped to, this should be
RewriteBase /
If your .htaccess file is in DOCroot/clean_urls/user then it should have
RewriteBase /clean_urls/user/
How do you want to decode URIs of the form http:://localhost/fred or are you only wanting do process URIs of the form http:://localhost/clean_urls/user/fred/. What about http:://localhost/clean_urls/user/fred/someverb?
Let me know and where you are putting you .htaccess then I can give you the right content.

.htaccess flat link

I want to to use flat links like this:
http://somedomain.com/about-us
http://somedomain.com/products-list
http://somedomain.com/product/item
Thus I've used the mod_rewrite rules like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9A-Za-z-]+)?$ /index.php?page=$1 [L]
RewriteRule ^([0-9A-Za-z-]+)/([0-9A-Za-z-]+)?$ /index.php?page=$1&value=$2 [L]
</IfModule>
The first two links are working fine. But whenever I visit the third type of links the images or the css or any js script that are linked as relative path eg. <img src="images/image.jpg"> and About Us.
The browser thinks it is in http://somedomain.com/product/images/image.jpg and http://somedomain.com/product/about-us.
The correct path should be http://somedomain.com/images/image.jpg and http://somedomain.com/about-us
And thus files with relative links are not working.
How to fix this issue? I don't want to use full path instead of relative for all linked files. Please suggest a way by doing some tweaks in the .htaccess
Try escaping your slash:
RewriteRule ^([0-9A-Za-z-]+)\/([0-9A-Za-z-]+)?$ /index.php?page=$1&value=$2 [L]

Resources