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]
Related
I'm trying to create clean urls using .htaccess on my website i have this
RewriteEngine on
RewriteRule ^([^/]*)/$ /full_posts.php?permalink=$1 [L]
RewriteRule ^category/([^/]*)/$ /search.php?category=$1 [L]
the first one is working correctly but the second one is not working, I'm writing both one after another. please help!
The rules you are looking for are:
RewriteRule ^([^/]*)/?$ /full_posts.php?permalink=$1 [L]
RewriteRule ^category/([^/]*)/?$ /search.php?category=$1 [L]
I have made the trailing slash optoinal in both the cases.
You can test your rules on:
http://htaccess.madewithlove.be/
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]
Although I already saw few posts about this thema, I am still having some problems to achieve what I want.
My old URL's were like:
http://myhostname.com/offer/1234_Nice_Offer_Cool_Whatever_HEllyeah.htm
And obviously I want to rewrite them to:
http://myhostname.com/offer/1234-nice-offer-cool-whatever-hellyeah.htm
This is the code I tried in .htaccess. This works fine to replace the underscores in URL's which they don't have ANY directory:
RewriteCond %{REQUEST_URI} \.htm$ [NC]
RewriteRule ^([^_]*)_+(.*)$ $1-$2 [E=underscores:Yes,N]
RewriteCond %{ENV:underscores} ^Yes$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
So, this paire of rules work fine with URL's like:
http://myhostname.com/1234_Some_Section_hellyeah.htm
But when I try the same code for URL's like:
http://myhostname.com/dir1/dir2/dir3/1234_Some_Offer_Or_Section.htm
Then, the server makes an infinite loop (I figure of the [N] flag...)
Basically, I would like to know in which way affects a per-directory URL to these rules and why am I getting this infinite loop. Thanks!
You don't need N flag. To replace underscore by hyphen recursively following code will work:
RewriteRule ^([^_]+)_(.+?\.htm)$ $1-$2 [L,NC,E=underscores:Yes]
RewriteCond %{ENV:REDIRECT_underscores} ^Yes$
RewriteRule ^([^_]+)$ /$1 [R=301,L]
I can successfully rewrite underscores to dashes with the following -- but I need the code to work ONLY for a certain directory, and I can't get that part to work.
WORKS:
RewriteRule ^([^_]*)_([^_]*)_(.*)$ /$1-$2-$3 [R=301,L]
RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L]
DOES NOT WORK:
RewriteRule ^/media/entry/([^_]*)_([^_]*)_(.*)$ /media/entry/$1-$2-$3 [E=unscors:Yes]
RewriteRule ^/media/entry/([^_]*)_(.*)$ /media/entry/$1-$2 [E=unscors:Yes]
RewriteCond %{ENV:unscors} ^Yes$
RewriteRule ^/media/entry/(.*)$ http://domain.com/media/entry/$1 [R=301,L]
When you remove all leading / from RewriteRule patterns all should work fine (as in the first two example which work).
The the following code:
RewriteRule ^media/entry/([^_]*)_([^_]*)_(.*)$ /media/entry/$1-$2-$3 [E=unscors:Yes]
RewriteRule ^media/entry/([^_]*)_(.*)$ /media/entry/$1-$2 [E=unscors:Yes]
RewriteCond %{ENV:unscors} ^Yes$
RewriteRule ^media/entry/(.*)$ http://domain.com/media/entry/$1 [R=301,L]
There is also nice online htaccess tester which will help you out verify your configuration e.g.
The tool has some limitations though i.e. currently does not have implemented certain features like %{REQUEST_FILENAME} or %{ENV:...} but for simple rules it should be fine.
I hope that will help.
After all those mod rewrite topics here at stackoverflow, I still haven't find an answer to my question. I have a topsite, and basically all I want to do is to change /index.php?method=in&cat=Half+Life (the "+" is a space) into /Half-Life .
Until now, I've succeeded changing /index.php?method=in&cat=Half+Life into /Half+Life.htm .
What I want is to make the .htm disappear and to change the "+" into "-".
Here's my code with what I'm working on in my .htaccess file :
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)\.htm$ /index.php?cat=$1 [L]
One more question: Is it more, how should I call it, "SEO-friendly", if I do it this way?
Thanks!
The problem with removing the .htm from it is that it then rewrites every single url. That would include urls for files that actually exist (like index.php). There are a couple approaches that you could take.
Assume that all your actual files have a dot (.) in the file name, rewrite everything else. If you do actually have files or directories that are named without an extension, this won't work. Or if you want some of your categories to have dots.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ([^\.]+)$ /index.php?cat=$1 [L]
You can have your rewrite rule ignore files that actually exist
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?cat=$1 [L]
I think it would be easier to change the php to replace the dashes with spaces, but if you really want to use htaccess:
RewriteRule (.*)-(.*)\.(htm)$ "$1 $2.$3"
RewriteRule (.*)\.htm$ /index.php?cat=$1 [L]
What you seem to be asking is to translate SEO-style URIs to internal parameter based ones. Unfortunately, the rewrite engine doesn't support global substitution so you need to cludge this, for example:
RewriteBase /
RewriteRule (\w+)$ index.php?cat=$1 [L]
RewriteRule (\w+)+(\w+)$ index.php?cat=$1-$2 [L]
RewriteRule (\w+)+(\w+)+(\w+)$ index.php?cat=$1-$2-$3 [L]
# and so on ..
But the easiest way to do this is to use a general catch all:
RewriteRule ([-+\w]+)$ index.php?cat=$1 [L]
And use a str_replace or preg_replace on your $_GET['cat'] inside your inddex.php.