I need to rewrite this url:
mysyte.com/index.php?lang=it
to:
mysite.com/it/index
without rewriting css href, javascript src and image src.
I tried many solutions but none worked, could someone help me?
thanks!
edit:
I tried:
RewriteEngine On
RewriteRule ^(.*)/(.*) $2.php?lang=$1
but this overwrites my css calls too.
then I tried:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*) $2.php?lang=$1
with the same result
EDIT :
In your .htaccess, write the following lines :
# Activate url rewriting
RewriteEngine on
# Rewrite all url of the form /2-letters-language-code/index to index.php/lang=2-letters-language-code
RewriteRule ^([a-z]{2})/index$ /index.php?lang=$1 [QSA,L]
The ([a-z]{2}) part of the regexp means "every group of two letters", so it will catch "it", but also "fr", "en"... and every other language code in two letters. If this is too general, you can replace it with just (it|en|fr) according to your needs.
And if you need to rewrite not just index to index.php, but whatever alphanumeric string, you can do :
RewriteRule ^([a-z]{2})/([a-zA-Z0-9_]+)$ /$2.php?lang=$1 [QSA,L]
Attention to not be too large in the second parenthesis, otherwise the rule will catch strings you don't want. For exemple,[a-zA-Z0-9_]+ means : every group of 1 or more alphanumeric character and underscores. It excludes slashes (/) and hyphens (-).
This was already answered here :
CSS not loading after redirect with htaccess rewrite rule
the easiest way to fix this is to specify the full path to your CSS file in the <head> of the HTML file.
If your mod_rewrite rule is modifying the path to where your CSS file is, you'd also want to place this before your RewriteRule:
RewriteCond %{REQUEST_FILENAME} !-f
This makes sure that the request doesn't match a file before rewriting it.
Related
Well I'm havin trouble creating .htaccess, anyway I have categories in my menu for example test1,test2,test3 that are the names of a folders, and what I'm trying to do is to point the second parameter of the url to index.php, for example http://myapp/test1 I want to be able from the index.php to have $_GET['cat'] = test1 and so on..
Here's what I've tried so far and didn't work:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)/$ index.php?cat=$1 [NC,L]
Any help with this? Much appreciated.
Try removing the trailer / character from your rewrite rule. ^(.*)/$ matches any URL that ends in a / like http://myapp/test/. You are probably looking for ^(.*)$ or to strip any leading slash ^/(.*)$. Note though that if index.php is accessed directly via f.ex. http://myapp/index.php this would also match and rewrite to http://myapp/index.php?cat=index.php and any images wouldn't also be directly accesible. So you might want to add some RewriteCond that first checks if the requested URL is an existing file with RewriteCond %{REQUEST_FILENAME} !-f before the RedirectRule to rewrite the URL.
Is there a way that I can rewrite a folder so that all the files under that folder follow the same rule? For example:
if i have a folder with say 5 php files (a.php, b.php, c.php, d.php, index.php) in it and i use the following rule:
RewriteRule ^products/storage/?$ /content/products/storage/index.php [QSA,L]
is there a way that I can get all the files to show to be accessed like: site.com/products/apples/a.php, site.com/products/apples/b.php, etc. without having to write a rule for each one?
I tried the following but it didnt work.
RewriteRule ^products/storage/?$ /content/products/storage/ [QSA,L]
I also need it to NOT overwrite my other rules such as:
RewriteRule ^products/storage/?$ /content/products/storage/product-name1/ [QSA,L]
RewriteRule ^products/storage/?$ /content/products/storage/product-name2/ [QSA,L]
any ideas?
Your problem is the trailing $ on the end of the regex. This will only allow a match if the full URI matches products/storage (with optional trailing slash) exactly. Instead, try the following and note the absence of the trailing $ character:
RewriteRule ^products/storage/? /content/products/storage/ [QSA,L]
This will match anything that starts with products/storage (with optional trailing slash). Alternatively, if you wanted to capture and re-use everything in the URI that followed products/storage/ you could try:
RewriteRule ^products/storage(/?.+)?$ /content/products/storage$1 [QSA,L]
UPDATE
Should you need to preserve other RewriteRules as your updated question suggests, you should look to add a RewriteCond condition like so:
RewriteCond !^products/storage/?$
RewriteRule ^products/storage(/?.+)?$ /content/products/storage$1 [QSA,L]
The RewriteCond tells the RewriteRule to only process if the condition is not met.
The main navigation of my site is coded like this:
<li>'.$value.'</li>'."\n";
But I would like the URL of the links to look like domain.co.nz/pagename, not domain.co.nz/index.php?pageId=pagename
How would you recommend I accomplish this?
Something like this should work:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?pageID=$1
First line turns on mod_rewrite. Second line sets the base URL to / (it's annoying, but you have to set it to the base path you're dealing with). Third and fourth lines make sure the request doesn't exist as a file, or as a directory. And the last line is the actual magic; basically it searches for "anything", captures what it finds in $1, and "rewrites" the URL to index.php?pageID=$1. If you learn to use regexes, you can do much more complicated things as well.
Yes, you can accomplish this with a .htaccess RewriteRule. In your .htaccess file, include:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?pageId=$1
This means:
If the REQUEST_FILENAME is not a valid file, redirect the entire URL to index.php?pageId=the entire URL
You'd then change your navigation to:
echo "<li>'.$value.'</li>'."\n";
Edit: I moved Trivikrtam's edit inline, see above. The RewriteRule should be index.php?pageId=$1 not /index.php?pageId=$1. Thanks #Trivikrtam!
This is what I have so far:
RewriteRule ^([A-Za-z0-9_]*)/?$ index.php?page=$1 [NC]
RewriteRule ^([A-Za-z0-9_]*)/([A-Za-z0-9_]*)/?$ index.php?page=$1/$2 [NC]
RewriteRule ^([A-Za-z0-9_]*)/([A-Za-z0-9_]*)/([A-Za-z0-9_]*)/?$ index.php?page=$1/$2/$3 [NC]
All of my css files are located in /ssi/.
The site structure itself is /index.php?page=$whatever
$whatever sometimes includes /'s, so if I go to /whatever/whatever2, with my current rules, it assumes the css is located in /whatever/ssi/*.css.
I hope all of that makes sense.
So, basically I just want to be able to write a condition that says "if it's a css file, don't apply these rules."
Thank you for any help I can get :).
The problem that you are experiencing is not that the mod_rewrite is being applied to css files, but rather that the paths in your html to your css is relative. The browser is simply requesting the css from the correct relative path. You really have two options.
Make your paths to your css relative to the domain instead of to the page (e.g. use /ssi/styles.css instead of ssi/styles.css)
Create a rewrite rule to redirect all css to the correct URL. (e.g. RewriteRule (*.css) /ssi/$1
Anticipating you're using Apache with mod_rewrite module, you should try the RewriteCond Directive. The following example excludes all the matches from RewriteCond for the following line with the RewriteRule Directive.
The rules are basically regular expression. The one in my example excludes everything which starts with either favicon.ico as well as with css, which is the folder, where my Stylesheets resides.
RewriteEngine On
RewriteCond $1 !^(favicon\.ico|favicon\.png|media|robots\.txt|crossdomain\.xml|css|js)
RewriteRule ^(.*)$ index.php/$1 [L]
Further reading:
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond
... it assumes the css is located in /whatever/ssi/*.css.
I just want to be able to write a condition that says "if it's a css file, don't apply these rules."
This translates directly to
RewriteCond %{REQUEST_URI} !^/whatever/ssi/[^/]+\.css$
RewriteRule ...
I guess my solution will be the most empirical one but it worked:
RewriteEngine On
RewriteBase /
RewriteRule ^([a-z0-9-]+)$ /?page=$1 [NC,L]
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)$ /?page=$1&action=$2 [NC,L]
That will translate http://example.com/?page=contact&action=send to http://example.com/contact/send
My page values were only alphanumeric and contain minus sign as word separator but you can replace the values to match whatever you need.
And, of course you need to prefix all of your js, css, img directories with a slash in the pages. So css/style.css would be /css/style.css or use absolute url.
I am having some trouble with my ReWrite code. Please note that the .htaccess file is in the subdomain folder (...public_html/subdomain/ )
I am simply trying to rewrite a page request:
http://subdomain.mysite.com/home
http://subdomain.mysite.com/index.php?page=home
My .htaccess file looks like this...
RewriteEngine On
RewriteRule ^/([A-Za-z0-9\-\_])$ /index.php?page=$1
Does anything jump out at you?
Your current rule probably works for urls one character long (after the slash)!
Add a + to signify one or more characters, or a * for zero or more
Try
RewriteEngine On
RewriteRule ^/([A-Za-z0-9\-\_]*)$ /index.php?page=$1
If you want to use the rules in a .htaccess file, you need to strip the contextual per-directory path prefix from the RewriteRule pattern. If the .htaccess file is located in the document root /, you need to strip the leading /.
Additionally you need to quantify the character set. Otherwise it would only describe one character.
So try this rule:
RewriteRule ^([A-Za-z0-9-_]+)$ index.php?page=$1
I think
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]
is ok ;)