I have two websites:
http://example.com
http://example.com/sub
I have a page, let's say:
http://example.com/sub/page1
Is there any way (using .htacces) to make it possible to access page1 via this url:
http://example.com/page1?
Redirect 301 won't do the work, because I don't need redirects. Is there any way to omit '/sub/' for certain urls, like http://example.com/sub/page1 ?
Yep, there is. Do the following within your .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# This would rewrite any URL that starts with /sub to /
RewriteCond %{REQUEST_URI} /sub
RewriteRule ^sub/(.*)$ /$1 [L]
<IfModule>
Edited to meet the OP's requirements.
Related
I have a web server with several Domains pointing to it. I want one domain lets name it www.example.com to go into the folder /example in my Doc Root (e.g. /var/www/example). I do this using this .htaccess code in my Doc Root:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteRule ^(/)?$ example [R=301,L]
</IfModule>
Now typing in the domain in the browser gets me to the correct location but instead of seeing www.example.com or example.com in the url bar of my browser I see [IP-Address]/example (e.g. 1.2.3.4/example). Of course I want the domain name www.example.com to show up and if I surf to a specific file e.g. /example/folder/index.php I want (www.)example.com/folder/index.php to show up.
I have tried some of the proposed fixes from other stack overflow questions regarding redirect to subfolder but all of them seem to elicit the above unwanted behaviour. Any help would be greatly appreciated.
Thanks
You can use these rules:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^(?!band/).*$ /band/$0 [L,NC]
Make sure to use a new browser for testing.
(?!band/) is a negative lookahead to prevent redirect when request already starts with /band/.
So I have a server where i have two django projects, both in different subfolders so i have
example.com/p1 and example.com/p2
I needed that when the user entered in example.com he was redirected to example.com/p1 but still remaning on example.com.
So i did that with the following code on my .htaccess.:
Options +FollowSymlinks
RewriteEngine On
RedirectMatch 301 ^$ http://example.com/p1/
RewriteRule ^$ /p1/?$ [L]
And that is working fine... For the home page but when i go to a sublink say the link is something like this
Foo
When i click on it it redirects me to example.com/foo/
I get why is doing that. Is there a way to make a rule that carries the url?
So say for example when the user came to the link example.com/foo/ it went to example.com/website/foo/ but for the user it showed as example.com/foo/ without havei the specific name of the url on the rule, so that the same rule apllied to both say the urls "foo" and "bar"?
You don't need Redirect directive here, just use mod_rewrite :
Try :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /p1/$1 [NC,L]
This will rewrite your urls :
http://example.com
to
http://example.com/p1
and
http://example.com/foo
to
http://example.com/p1/foo
I want to use htaccess to redirect certain pages to its equivalent in another domain, other pages redirect to homepage.
Like that:
http://old-domain.com/certain-page redirected to: http://new-domain.com/certain-page
http://old-domain.com/another-certain-page redirected to: http://new-domain.com/another-certain-page
Only these pages will be redirected, otherwise, pages have to be redirected to the new domain home page.
http://old-domain.com/non-certain-page redirected to: http://new-domain.com
This is my try:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^certain-page$ http://new-domain.com/certain-page [R=301,L]
RewriteRule ^another-certain-page$ http://new-domain.com/another-certain-page [R=301,L]
</IfModule>
But don't know how to exclude other pages.
Any help here?
Thanks in advance!
I'm not sure what you mean by
But don't know how to exclude other pages
But you are already excluding the pages when you create specific rules for them. This should work.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#redirect specific pages
RewriteRule ^certain-page$ http://new-domain.com/certain-page [R=301,L]
RewriteRule ^another-certain-page$ http://new-domain.com/another-certain-page [R=301,L]
#Redirect everything else to homepage
RewriteRule ^(.*)$ http://new-domain.com/ [R=301,L]
</IfModule>
Let me know how this works for you. If you need to clear you cache before trying these new rules.
Another answer was mentioned here, thank to anubhava
With redirect without need to use Rewrite :
RedirectMatch 301 ^/.+ http://www.newdomain.com/
So, the answer can be also:
#Redirect specific pages
redirect 301 /certain-page http://new-domain.com/certain-page
redirect 301 /another-certain-page http://new-domain.com/another-certain-page
#Redirect everything else to homepage
RedirectMatch 301 ^/.+ http://new-domain.com
I would like to do a simple 301 direct on some old URLs that have parameters to them.
e.g.
/dir/page1/?id=4590
/dir/page1/?id=45
/dir2/page5/?id=4590
/dir2/page9/?id=45
/page90/?id=45etc
...
I want a 301 redirect of the above urls to the corresponding urls w/o parameters:
/dir/page1
/dir2/page5
/dir2/page9
/page90
My experience with .htaccess is limited. What would be the simplest and the most correct way to enable the above redirects to actually work.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^id= [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
I want to redirect all user page requests to a page on the same domain.
For example, I have an "under construction, BRB" page that I want all users to see when they try to access ANY page on the site.
I tried using this:
Redirect 302 / http://www.domain.com/index2.php
What that does is try to apply the redirect to the index2.php page as well and it gets stuck in a loop where the user then sees this until the browser stops.
http://www.domain.com/index2.phpindex2.phpindex2.phpindex2.php etc., etc,
Any idea on how to write that rule to except that page?
You have to exclude the file you want to redirect to. Here’s an example with mod_rewrite:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule !^index2\.php$ /index2.php [L,R=302]
</IfModule>
To prevent endless looping (index2.php <--> index2.php):
Only redirect If URL to be redirected does NOT contain the string 'index2.php'
RewriteCond %{QUERY_STRING} !index2.php
RewriteRule ^(.*)$ /index2.php [L,R=301]
You could use mod_rewrite
<IfModule mod_rewrite.c>
RewriteCond {REQUEST_URI} !=/index2.php
RewriteEngine on
RewriteRule ^.*$ /index2.php
# End .HTACCESS
</IfModule>
I'd be more inclined to use the slightly different
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/.*$ /index2.php [R=307,L]
</IfModule>
This will let you return a moved temporarily status for the redirect.
Omitting this set of flags means that mod_rewrite will return a 302 Found status by default.
HTH
cheers,