Rewrite cond not carrying arguments .htaccess - apache

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

Related

apache redirect to remove a subfolder from the url

I have issues redirecting a specific url within the same domain
Example, I want to redirect
http://example.tld/folder/*
to
http://example.tld/*
where * is a wildcard
Basically you will need this type of code in your .htaccess file:
RewriteEngine On
RewriteRule ^/folder/ http://domain.tld/? [R=301,L]
This should redirect your current url http://domain.tld/folder to http:/domain.tld/
Or:
RewriteEngine On
RewriteRule ^folder/(.*)$ /$1 [R=301,NC,L]
Please try and see which one works best for you

How to add a couple of custom exceptions and redirect all the rest of url-s to another url in htaccess?

There is two sites, old.com and new.com. In the .htaccess of old.com I have some custom URL's that should be redirected like:
*1. example.html -> new/something/
example2 -> new/stgelse
example-3 -> new/another.html
/admin should not redirect anywhere.*
All the rest of url's of old.com should redirect to new.com(index).
So far I've achieved to manage to redirect everything regarding old.com to new.com, with Redirect 301 / http://new.com/.
I've tried to add exceptions, like writing Redirect 301 something.html http://new.com/whatever, but it just redirected to new.com(index).
Also, I've tried RewriteCond %{REQUEST_URI}, but it seems like Redirect 301 / http://new.com/ overwrites everything. How could I successfully apply my custom exceptions? I have like 5 of them only.
Even if I only try RewriteCond %{REQUEST_URI} !^/admin
RewriteRule (.*) http://www.new.com/$1 [R=301,L], then I navigate to old.com/admin, it redirect to the index of new.com.
Don't mix Redirect with RewriteRule they are two diffrent modules.
You can use this :
RewriteEngine on
#example.html => http://newsite.com/new/something
RewriteRule ^example.html$ http://nesite.com/new/something/ [L,R]
#example2.html => http://newsite.com/new/another.html
RewriteRule ^example2.html$ http://newsite.com/new/another.html [L,R]
#redirect everything except /admin
RewriteRule !^admin http://newsite.com/ [L,R]
The first part of the answer regarding the way to add exceptions is already given. It might be a full answer at other cases, but in the case of Magento the final condition, "everything else should redirect to new.com except admin" looks the following way:
## RewriteRule !^/admin https://www.new.com/ [L,R=301]
RewriteCond %{REQUEST_URI} !^/admin(/|$)
RewriteRule ^([\w/]*)$ http://www.new.com [L]
The first, commented condition is true only for precise "admin", but at Magento the admin might get pre- or appended, without showing anything else than "admin" in the navbar. This way it works perfectly.

Opencart 301 Redirects

Having a problem with redirect in a .htaccess file on an Opencart store.
It seems that any URL with /index.php?_route_= isn't getting redirected.
For example, this works:
redirect /old-url-here http://example.com/new-url?
This doesn't:
redirect /index.php?_route_=some-url.asp http://example.com
Any idea or suggestions as to what might get this to work?
You can't match against the query string using mod_alias' Redirect directive. You'll need to use mod_rewrite, and if you use mod_rewrite, you're probably going to want to stop using mod_alias altogether.
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} route=some-url\.asp
RewriteRule ^index\.php$ http://example.com/
Another thing is - apart from Jon's answer - that URLs like index.php?_route_=some-keyword are used/created only internally and only in case you have the SEO turned on. In this case, you click on a link with URL like http://yourstore.com/some-keyword and this URL is rewritten into index.php?_route_=some-keyword.
Therefore you shouldn't be creating URLs like that on your own nor try to redirect from them. If you need to redirect, catch the first SEO URL to redirect it.
We do not know where did you put your changes into the .htaccess file, but if you take a close look at this rewrite rule
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
not only you'll find out it is the last rule there, but it also has this L switch which is telling Apache server that if this rule is matched, do a URL rewrite(, attach the query string [QSA] and) stop matching other rules [L] - this is the Last one.
If you need to redirect only a specific URL, do it the same way as redirect for sitemap.xml is done (for example) and place it before the last rewrite rule:
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
# ...
# your new rule here - while the URL in the link is http://yourstore.com/some-url.asp
RewriteRule ^some-url.aspx$ index.php?route=some-folder/some-controller [L]
# ...
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

CakePHP 301 Redirects appending extra query string parameter

I am trying to redirect old asp pages on a domain name so they link to their respective pages on the CakePHP version (using 1.3). The domain name is the same. These redirects are being added so results in search engines go to the new Cake page.
I have a bunch of Redirect 301's in my /.htaccess file (app/.htaccess and app/webroot/.htaccess are default).
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
Redirect 301 /contacts.asp http://domain/contacts
Redirect 301 /users.asp http://domain/users
</IfModule>
But for some reason, when I go to any of the urls to test the redirects, it appends an extra query string parameter. For example:
Going to: http://domain/contacts.asp results in http://domain/contacts?url=contacts.asp
So the redirect is working but it is appending the url query string parameter. I don't want to completely remove all query string parameters because some of the old asp links have query string parameters that I would also like passed to the corresponding Cake page.
I believe the "url" query string parameter is coming from the app/webroot/.htaccess file as seen:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Note that when I put all my 301 redirects into my Virtual hosts file, the url parameter is not appended. I would like to keep all these redirects in my .htaccess file. How can I prevent this url query string parameter from being appended?
This looks like a case of mod_rewrite and mod_alias stepping over each other. The URI processing pipeline doesn't end when a Redirect directive is applied, it continues through the pipeline and mod_rewrite does its thing. Unfortunately, the end result isn't always what you want. You could just stick with mod_rewrite and drop the Redirect directives.
You can remove the 2 Redirect directives and add these 2 RewriteRules above the ones that map requests to the app/webroot:
RewriteRule ^contacts\.asp$ http://domain/contacts [L,R=301]
RewriteRule ^users\.asp$ http://domain/users [L,R=301]

Access webpage from subdirectory as root page

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.