I would like to RewriteRule for a Specific query attribute
e.g.
https://website.com/shop/?req=cosmetics
to:
https://website.com/shop/cosmetics
I achieved that by doing:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^shop/(.+)$ shop/?req=$1 [L,QSA]
My problem is that this will replace any query attribute...
What i'm looking for is to perform the RewriteRule explicit on '?req=' and let any other attr e.g. '?foo=' just to pass and execute.
I know that this is possible with RewriteCond %{QUERY_STRING} but i can't make it work...
Thanks
The solution is to change first rule and do something like this,otherwise server can't predict itself other query string except the existing one req :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^shop/(.*)/(.*)$ /shop/?$1=$2 [L,QSA]
So, the Request https://website.com/shop/?req=cosmetics will be from https://website.com/shop/req/cosmetics request and .as you suggested , https://website.com/shop/?foo=cosmetics will be from https://website.com/shop/foo/cosmetics request
If you want to apply this code from shop directory .htaccess file :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/shop/merchant
RewriteRule ^(.*)/(.*)$ /?$1=$2 [L,QSA]
it will exclude merchant as well , Test it .
Related
I want to use URL rewrite with my .htaccess to redirect my non pretty URL:
http://example.com/_new/url.php?module=planning&action=see&id=2
to this one:
http://example.com/_new/url/planning/see/2
So I used:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ url.php?module=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ url.php?module=$1&action=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ url.php?module=$1&action=$2&id=$3 [L]
But it doesn't work.
For this specific url rewrite, You might need to replace your [RewriteRule] to something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^_new/url/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$
_new/url.php?module=$1&action=$2&id=$3 [NC,L]
From which you can access: http://example.com/_new/url.php?module=planning&action=see&id=2
http://example.com/_new/url/{module_name} = http://example.com/_new/url.php?module={module_name}
The same applies for $2, $3 as well.
[NC] = Case insensitive : This flag tells apache that there will be [no case]
[L] = If this rule was already processed to begin with, No additional rules will be added in the current url rewriting process
You can learn more about here: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
Hope this helps.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]
the above i have in my htacces file which rewrites directory/page to index.php?id=directory/page
thats working fine.
I also want to be able to add the following to it:
domain.com/sections/page rewrites to index.php?section=1&id=page
domain.com/sections/page2 rewrites to index.php?section=1&id=page2
domain.com/page rewrites to index.php?id=page
the ID is going to be different for each page
You have to take a look at RewriteCond and RewriteRule directives.
That's a sample .htaccess based on your edit.
RewriteEngine On
# This will process the /sections/(.*) requests, ?section=1 will be appended to query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sections\/(.*)?$ index.php?section=1&id=$1 [L,QSA]
# This will process the other requests, as it does now.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]
I need to simultaneously do two things with htaccess.
I need to take a URL like:
http://client.example.com/123
and rewrite the directory to a param, and simultaneously add another subdomain to the url so it looks like this:
http://client.qa.example.com/?param=123
This does the param bit correctly, but I can't figure out how to add the subdir:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[^/]+/?$
RewriteRule ^([^/]*)/?$ /?param=$1 [L]
You can examine the host header using a RewriteCond and extract the relevant parts of the name. Use them in the rewrite. Back references to matches in RewriteConds appear as %n
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} (.+?)\.(.*)
RewriteRule ^([^/]*)/?$ http://%1.qa.%2/?param=$1 [R,L]
(.+?)\.(.*) will do a match on everything up to the first . and then everything to the end. So client and example.com will respectively be in %1 and %2
If your .htaccess is in the root of client.example.com, it should be a simple redirect. Of course the directory has to be a fake directory or this won't redirect.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ http://client.qa.example.com/?param=$1 [R=301,QSA,L]
You can use the following to match (check for htaccess syntax):
(http://[^.]+\.)([^/]+/)([^/]*)/?$
And replace with:
$1qa.$2?param=$3
See DEMO
Finally got it working using:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} (.+?)\.(.*)
RewriteRule ^([^/]*)/?$ http://%1.qa.%2/?param=$1 [R,L]
Now I just have to figure out how to work in 2 parameters, given that param 2 isn't always going to be present.
My all URLs formats are like these:
http://example.com/category?format=feed
http://example.com/category/sub_category/?format=feed
for example: http://example.com/computer/cpu/?format=feed
Is there a way via htaccess to change top URLs to these:
http://example.com/category/feed
http://example.com/category/sub_category/feed
example1: http://example.com/computer/cpu/feed
example2: http://example.com/computer/feed
These rules should meet both requirements. But allows you to change the category and subcat as long as the normal URLs don't change and format is always a key in the query string.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/(.+)/(.+)$ /$1/$2/?format=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/(.+)$ /$1/?format=$2 [L]
Then you should be able to use these URL types.
http://example.com/category/feed
http://example.com/category/sub_category/feed
The following should achieve what you are looking for:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^format=(.*)$
RewriteRule ^(.*)/(.*)/$ /$1/$2/%1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^format=(.*)$
RewriteRule ^(.*)$ /$1/%1? [L,R]
The above will turn:
http://example.com/category?format=feed to http://example.com/category/feed
and
http://example.com/category/sub_category/?format=feed to http://example.com/category/sub_category/feed
I'm trying to convert subdirectories to a search query. So basically:
example.com/pizza
would be converted to:
example.com/?s=pizza
I have a basic knowledge of htaccess and I have done similar things before, however this time I cannot make it work. This is the problem:
This works:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /results/?s=$1 [L,QSA]
However when I point the rewrite target to the root the user is redirected to "example.com" without the query string.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /?s=$1 [L,QSA]
How can I rewrite "example.com/pizza" to "example.com/?s=pizza" ?
You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(?:.*)?/([^/]+)/? [NC]
RewriteRule .* /?s=%1 [L]
Maps silently:
http://example.com/any/number/of/folders/parameter with or without trailing slash.
To:
http://example.com/?s=parameter
All strings are assumed to be variable.
For permanent and visible redirection, replace [L] with [R=301,L].