Swap 2 letter part of a url in htaccess file - apache

Below is my htaccess file (complete). What I need to do is rewrite it so that images for say /uk/images/file.jpg become /images/uk/file.jpg. Although its not erroring its not working either. Am I missing something?
RewriteEngine on
RewriteRule ^/([A-Za-z]{2})/images/(.*)$ /images/$1/$2 [L]
RewriteCond $1 !^(index\.php|css|jscript|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Try :
RewriteEngine on
RewriteRule ^/?([A-Za-z]{2})/images/(.*)$ /images/$1/$2 [NC,L]
RewriteCond %{REQUEST_URI} !^/images/([^/]+)/ [NC]
RewriteCond $1 !^(index\.php|css|jscript|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Related

.htaccess: How to rewrite all urls to index.php excepting urls to one directory

I'm trying to rewrite all urls to index.php excepting for urls inside localhost/src/...
This is the htaccess I've been using so far:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^ /index.php [L]
UPDATE:
Nevermind I just came up with a solution:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/src
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^ /index.php [L]
However I would still like to know if there is a better way of doing it
Posting the update as an answer:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/src
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^ /index.php [L]

htaccess mod_rewrite - Trailing Slash and loop of redirects

Here is my htaccess code:
RewriteEngine On
RewriteRule ^s/(.*)/(.*) /index.php?search=$1&category=$2 [L,QSA]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)&category=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/%2/? [R=301,L]
I need to make a rewrite rule like this:
http://mywebsite.com/s/query+term => http://mywebsite.com/?search=query+term
OR if there is a category
http://mywebsite.com/s/query+term/Category => http://mywebsite.com/?search=query+term&category=Category
I need also to redirect all the old urls to the new one.
With this rules all that I can obtain is to have the search term and category name joined togheter. In a nutshell it is as if I had always:
http://mywebsite.com/?search=query+term/Category
If I remove all the conditions (RewriteCond), leaving only the rewrite rule with the addition of a trailing slash:
RewriteEngine On
RewriteRule ^s/(.*)/(.*)/ /index.php?search=$1&category=$2 [L,QSA]
Reaching the url of my interest in a direct way, the whole thing works. But I will not have the redirect..
So I have tried to set a rule like this:
RewriteEngine On
RewriteRule ^s/(.*)/(.*)/ /index.php?search=$1&category=$2 [L,QSA]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)&category=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/%2/? [R=301,L]
But with this rule I get just a loop of redirects.
You can use the following rules :
RewriteEngine on
#1--Redirect from "?search=foobar&cat=cat" to "/s/foobar/cat" --#
RewriteCond %{THE_REQUEST} /\?search=([^&]+)&cat=([^\s]+) [NC]
RewriteRule ^ /s/%1/%/%2? [NC,L,R]
#2--Redirect from "/?search=foobar" to "/s/foobar" --#
RewriteCond %{THE_REQUEST} /\?search=([^/\s]+) [NC]
RewriteRule ^ /s/%1? [NC,L,R]
#1--Rewrite "s/foobar/cat" to "/?search=foobar&cat=cat"--#
RewriteRule ^s/([^/]+)/([^/]+)/?$ /?search=$1&$category=$2 [NC,L]
#2--Rewrite "s/foobar" to "/?search=foobar"--#
RewriteRule ^s/([^/]+)/?$ /?search=$1 [NC,L]

url rewrite in php for change posts url

I want change url:
http://example.com/posts.php?action=view&id="a number"
to
http://example.com/posts/"a number"
I write these into the .htaccess file but they don't work:
RewriteEngine On
RewriteRule ^posts/([0-9]+)/?$ posts.php?action=view&id=$1 [NC,L]
RewriteRule ^posts/?$ posts.php [NC,L]
It'll be like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /(posts)\.php\?action=view&id=(\d+) [NC]
RewriteRule ^posts\.php$ /%1/%2? [R=301,L,NC]
RewriteCond %{THE_REQUEST} ^GET\ /(posts)\.php\ HTTP [NC]
RewriteRule ^posts\.php$ /%1/? [R=301,L,NC]
RewriteRule ^(posts)/(\d+)/?$ /$1.php?action=view&id=$2 [L]
RewriteRule ^(posts)/?$ /$1.php [L]
You can try below in your .htaccess file
.htaccess file code:
RewriteEngine On
RewriteRule ^posts/([0-9]+) /posts.php?action=view&id=$1 [QSA,L]
QSA means that if there's a query string passed with the original URL, it will be appended to the rewrite.
L means if the rule matches, don't process any more RewriteRules below this one.

.htaccess redirect for all files but with exceptions

I know this has been asked thousands times, but I've been googling for three hours without any result. So I'm asking here. I'm creating a website. All the content is in the folder /subfolder. Now I want to redirect all the requests to another domain (let's say domain.com), at the exception of the files that I actually use. It may seem weird but it makes sense in my situation. So what I have at the moment is
RewriteEngine on
RewriteRule ^account$ account.php
RewriteRule ^home$ myhome.php
RewriteRule ^options$ options.php
RewriteRule ^login$ login.php
RewriteRule ^links$ editlinks.php
RewriteRule ^help$ howto.php
RewriteRule ^$ index.php
RewriteRule ^forgot$ forgot.php
RewriteRule ^r$ redirect.php
RewriteRule ^r/(.*)$ redirect.php?id=$1
RewriteCond %{REQUEST_URI} !(login.php|login)
RewriteRule (.*) external.php?parameter=$1 [L]
How can I do this ? the above code always redirects me to domain.com.
Thank you in advance
Make sure to place redirect just below RewriteEngine On and use THE_REQUEST instead of REQUEST_URI like this:
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{THE_REQUEST} !/media [NC]
RewriteCond %{THE_REQUEST} !/(login|signup)(\.php)?[?\s] [NC]
RewriteRule (.*) http://domain.com/$1 [R=301,L]
RewriteRule ^home$ myhome.php [L]
RewriteRule ^links$ editlinks.php [L]
RewriteRule ^help$ howto.php [L]
RewriteRule ^(account|login|options|forgot)/?$ $1.php [L,NC]
RewriteRule ^r$ redirect.php [L]
RewriteRule ^r/(.+)$ redirect.php?id=$1 [L,QSA]

.htaccess Rewrite GET-Data into GET-Data

i want to rewrite http://192.168.2.101/game.php?village=567919&screen=overview into http://192.168.2.101/index.php?dir=game&page=overview&village=567919 can anyone tell my why this is not working well and how i do it right?
my .htaccess code
RewriteEngine On
RewriteRule ^$ index/
RewriteRule ^(.*)/$ index.php?dir=main&page=$1 [L,QSA]
RewriteRule ^game.php?village=(.*)&screen=(.*)$ index.php?dir=game&page=$2&village=$1 [L,QSA]
You have to use %{QUERY_STRING} to parse query string parameters.
Replace your code by this one in your htaccess
RewriteEngine on
RewriteCond %{QUERY_STRING} ^village=([^&]+)&screen=([^&]+)$
RewriteRule ^game\.php$ /index.php?dir=game&page=%2&village=%1 [L]
RewriteRule ^(.+)/$ /index.php?dir=main&page=$1 [L]
RewriteRule ^$ /index.php [L]