I need to rewrite only 1 specific URL, to display to visitors specific content: I tried something like, this:
RewriteEngine on
RewriteCond %{REQUEST_URI} example.com/test/2_5/page.html
RewriteRule ^(.*)$ example.com/tt.html [R,L]
I need to rewrite all requests to:
http://example.com/test/2_5/page.html
to
http://example.com/tt.html
how to do this?
thanks,
Redirect /test/2_5/page.html /tt.html
Ok, mod_rewrite
RewriteRule ^/test/2_5/page.html /tt.html [L]
Remove first / if using .htaccess in the site's root folder, not the .conf file. And, typically, the final url should be the full one, with http:// and domain, but this one will work too. If you want to do everything by the rules then
RewriteRule ^/test/2_5/page\.html$ http://example.com/tt.html [L]
Related
I'm not very experienced with htaccess. today I fail in writing an .htaccess to rewrite a url.
What I want:
My project is hosted on a subdomain e.g. sub.domain.com
My htaccess should manage:
sub.domain.com should redirect to www.domain.com (Redirect 301, that's clear)
subfolders should be put as GET variable e.g. sub.domain.com/test to www.domain.com?var=test
the subfolder /admin shouldn't be redirected
Thanks in advance for tips!
This should do it for you:
RewriteEngine on
RewriteCond %{HTTP_HOST} =sub.example.com
# Following condition added to support changed requirement, see comments
RewriteCond %{QUERY_STRING} !(?:^|&)var=
RewriteCond %{REQUEST_URI} !^/admin/
RewriteRule ^(.*)$ http://www.example.com/?var=$1 [R=301,L]
This will drop any query string that was in the original request. If you want to keep any query string, and just append 'var=...' to it, then add QSA to the flags at the end of the last line, separated by a comma.
I've made some search about .htaccess Rewrite function, but I couldn't find any example with an html reference.
I'm trying to redirect from my example.com/products/category/ link to another page.
Here's the link what I'd like the .htaccess file to redirect:
<a href="example.com/products/category/?category=bathroom">
I'd like to achieve this style:
example.com/products/category/bathroom/
Here's my .htaccess ReWriteRule:
RewriteRule ^products/([A-Za-z0-9-]+)/?$ /products/category.php/?category=$1 [L] # Process category
Note that I'm using two different php pages for both. page-category.php and category.php
I've places it after
RewriteEngine On
RewriteBase /
but still it doesn't work.. The only time something happened when I tried some combinations, it threw back an internal server error. Is there a problem with my RegEx or am I not doing the linking right ?
EDIT: I'm using wordpress, I forgot to mention that, sorry.
You need to use Redirect 301
RedirectMatch 301 ^example.com/products/category/?category= /example/home/page-category.php
Or you can use mod_rewrite instead:
RewriteEngine On
RewriteRule ^example/products/category/?category= /example/home/page-category.php [L,R=301]
same you can do for another file.
as i understood from your question you need something like this in category directory .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) category.php?category=$1 [QSA]
Now anything that is not a directory or a file inside category directory will be rewriten like /category/bathroom will be rewriten to (without redirection) /category.php?category=bathroom
Greets:
I have an old piece of code in a /work directory that I have to keep there. I'd like to redirect all requests to the /work directory to the site root, *except for url's that look like this:
/work/index.php?option=com_career&view=career &
/work/administrator
How would I go about doing this in .htaccess?
Thanks in advance.
Chris
Try adding these rules to the htaccess file in your document root:
RewriteEngine On
# pass through this URL unchanged
RewriteCond %{QUERY_STRING} ^option=com_career&view=career
RewriteRule ^work/index\.php$ - [L]
# pass through this URL unchanged
RewriteRule ^work/administrator - [L]
# redirect everything else
RewriteRule ^work/ / [L,R=301]
On my site I have multiple URLs like this:
Main Page:
mysite.com
mysite.com/?content=about
mysite.com/?content=posts&page=2
Subfolders:
mysite.com/subsite/
mysite.com/subsite/?content=about
mysite.com/subsite2/?content=posts&page=2
I'd like to make clean these up to be:
mysite.com/about/
mysite.com/posts/2
mysite.com/subsite/about/
mysite.com/subsite/posts/2
Now, I've been able to use mod_rewrite for one variable, and some other simple things, but I'm not exactly sure how to accomplish this. When I use:
RewriteRule ^([a-z]+)/([a-z0-9-]+)/?$ /?content=$1&page=$2 [L]
It recognizes the sections of the URL as variables, but it also sees the subsite as a variable, and attempts to plug in 'subsite' for 'content'.
Would anyone be able to point me in the right direction?
You could tweak you existing rule to allow for an OPTIONAL subsite/ or subsite2/ prefix e.g.
RewriteRule ^(subsite/|subsite2/)?([a-z]+)/([a-z0-9-]+)/$ /$1?content=$2&page=$3 [L]
Or just add a rule to handle the subsites before the existing rule e.g.
RewriteRule ^(subsite/|subsite2/)([a-z]+)/([a-z0-9-]+)/$ /$1?content=$2&page=$3 [L]
RewriteRule ^([a-z]+)/([a-z0-9-]+)/?$ /?content=$1&page=$2 [L]
I ended up getting this to work by putting another .htaccess file in the subsite folder.
The root .htaccess has:
RewriteEngine On
RewriteRule ^([A-Za-z0-9]+)/?$ ?a=b&content=$1 [L]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/?$ ?a=b&content=$1&page=$2 [L]
In the subsite .htaccess I have: (same as root .htaccess)
RewriteEngine On
RewriteRule ^([A-Za-z0-9]+)/?$ ?a=b&content=$1 [L]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/?$ ?a=b&content=$1&page=$2 [L]
If you do not need to alter the subdirectory's url, add only RewriteEngine On to the subsite's .htaccess. This basically overrides the root .htaccess rewrite rules, and loads the page from the subfolder.
I need help with my mod_rewrite for a site im currently working on.
Let's say I have this site http://example.com
And I want to be able to make any value after the / to route to page.php like below
http://example.com/value1
http://example.com/value2
to point to
http://example.com/page.php?id=value1
http://example.com/page.php?id=value2
,respectively.
But, not route to that page when im pointing to "admin"
http://example.com/admin/
I've tried
RewriteEngine on
RewriteCond $1 !^(admin)
RewriteRule ^(.*)$ /page.php?id=$1 [L]
But it isn't working. Any thoughts?
$1 is not available at the time of the Condition. I believe what you are looking for is close to:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^admin/.*
RewriteRule ^(.*)$ page.php?id=$1 [L]
this rule causes everything not starting with admin/ to go to /page.php. I don't believe the %{param} is optional. Using RewriteBase / means you do not to have prepend / on /admin and /page.php; it may actually fault if you use /page.php instead of page.php
If you have means of accessing the server values, then the final rule can be:
RewriteRule . page.php [L]
You can find the called url in the REQUEST_URI
This of any help?
.htaccess mod_rewrite - how to exclude directory from rewrite rule