Rewrite rule with exception - php

Here is my directory structure
main site:-
www.magentosite.com
Wordpress site:-
www.magentosite.com/wordpresssite
And Here is my htaccess rule for redirect. If User goes to wordpress site then they will redirect to magentoSite.
RewriteCond %{REQUEST_URI} !^WordpresSITE/
RewriteRule .? http://www.MagentoSITE.com/ [L]
PROBLEM
Above should work as it is but I want to put if user goes to admin side of wordpress then yes they should not redirect. In short www.magentosite.com/wordpresssite/wp-admin/... should not redirect.

The below code should go inside the .htaccess on the WordPress folder:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/wordpresssite/(wp-login\.php|wp-admin) [NC]
RewriteRule ^ http://www.MagentoSITE.com/ [R=302,L]
Basically what I am doing is if the URL start with:
www.magentosite.com/wordpresssite/wp-login.php
Or starts with:
www.magentosite.com/wordpresssite/wp-admin
It should not redirect, if it doesn't then it redirects.

Related

Magento, redirect URLs with index.php to URLs without it

I am setting up a Magento website which is not inside of any sub-directory such as /shop.
Before every URL on the website had the index.php append next to the domain name, for e.g the admin panel was example.com/index.php/admin.
I enabled Mod_rewrite in Magento's admin panel and added an .htaccess file with the following to make sure that example.com/admin actually goes to the admin panel and so on. It does work.
Here's the htaccess configuration:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
The problem is that when I manually type example.com/index.php/admin it takes me to the admin panel as expected but the URL stays that, I expected it to redirect to the right URL which is example.com/admin but I am not able to.
Do I have to add any configurations to the htaccess or is it Magento related?
You can use the following rewrite rules to do this:
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
This will check if the url contains index.php and if it does then redirect to the url without index.php.

'Fetch as Google' in Webmaster tool says 'Redirected'

Google webmaster tool shows me this for my website. https://ibb.co/cnCOfk
This is a WP website, and my guess is it's because the redirection from non-www to www is not happening. Google fetches with www and then my website gets redirect too non-www domain. How do I fix this problem?
I found online that I can solve this with .htaccess. Now, I added last 2 lines (above IfModle ends). Currently, my .htaccess looks like this.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
# END WordPress
Once I add this, website goes in infinite loop of "www.domainname.co" to "domainname.co" to "www.domainname.co" and so on.
How do I solve this problem? Thank you in advance.
It would seem this redirect is occurring within WordPress itself - that is where you need to fix this. You can then repeat the same redirect in .htaccess if you wish, as that would be more efficient.
But if you do the opposite redirect in .htaccess then you'll naturally get a redirect loop.
EDIT: And if you implement this redirect in .htaccess, it should go before the WordPress front controller.

WordPress Redirect all file downloads to subdomain

I have a new WordPress site, example.com, with the old Drupal site archived at archive.example.org. Many files on the old site had a URL like so (ex: example.org/files/foo.pdf), but all those links out there in the internet are breaking because all those file downloads need to be redirected to archive.example.org/files/foo.pdf.
Can creating redirects (I'm guessing .htaccess is the best bet) work for file downloads as well? Is there a simple redirect to send all old links to the archive subdomain?
I have tried:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/files/
RewriteRule ^ http://archive.example.org%{REQUEST_URI} [R,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
...with no luck so far.
Thanks for any help!
If you want to redirect all files/* requests to the new domain and keep everything else from WordPress, you need a condition for the rule, which checks for files
RewriteCond %{REQUEST_URI} ^/files/
RewriteRule ^ http://archive.example.com%{REQUEST_URI} [R,L]
When everything works as it should, you may replace R with R=301. Never test with R=301.

.htaccess Rewrite Parent Folder to Subfolder

I am looking to rewrite the url so if someone lands on a parent folder, they will be redirected to its subfolder. http://www.domain.com/portfolio should take me to http://www.domain.com/portfolio/example
RewriteEngine is on, and there are other RewriteRules in the file, which are working. I have tried the following rewrites, but none perform the redirect:
RewriteRule ^/portfolio/(.*)$ /portfolio/example/$1 [R=301,L,QSA]
and
RewriteCond %{HTTP_HOST} ^(www.)?domain.com/portfolio$
RewriteRule ^(/)?$ portfolio/example[L,QSA]
and
RewriteRule ^portfolio/(.*) http://www.domain.com/portfolio/example/$1 [R=301,L,QSA]
and
RewriteRule ^portfolio$ http://www.domain.com/portfolio/example[R=301,L,QSA]
RewriteRule ^portfolio/(.*) http://www.domain.com/portfolio/example$1 [R=301,L,QSA]
What is the proper syntax for rewriting a url so that if a visitor lands on the parent portfolio folder, they go to the portfolio/example subfolder?
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^portfolio(/(?!example).*|)$ /portfolio/example$1 [R=301,L,NC]

redirecting all dynamic urls to index page

as i want to redirect all dynamic urls like http://www.mysite.com/play.php?id=4820 to http://www.mysite.com/
i have so many dynamic urls i dont want my user to see the page like page not found etc. so whenever a user try to access the dynamic url like the above he should be redirected to home page. please can anyone tell me how to achieve this using .htaccess
Regards,
phphuger.
Enable mod_rewrite and put this code in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+play\.php[\s\?] [NC]
RewriteRule ^ / [L,R]
I think this should do.
RewriteRule ^\?.*$ http://www.mysite.com
EDIT2
Ok, now I think understand your question.
You have url at play.php that used to handle all your urls and you want them to be redirected to the homepage.
Here are two possible solutions. This is the first:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/play.php* [NC]
RewriteRule . /? [L,R=301]
</IfModule>
The above will redirect any requests to play.php to your home page using a 301 redirect, which means the redirect is permanent. This is different than a 302 redirect which is temporary. So it is important to redirect using 301.
Also in this rewrite rule, we don't care if the url exists or not. So you don't need to have the actual play.php file or anything. It just matches based on the url.
On the line for RewriteRule, there is a question mark at the end, this is to erase the query string from the url on redirect. I'm assuming you don't want the query string to carry over.
The [NC] is for case insensitive matching, so /PlAy.php would be redirected also
An alternative is use the Redirect directive:
Redirect 301 /play.php http://www.mysite.com/
This will do a 301 permanent redirect to your homepage if the user tries go to play.php. The only downside of this is that the query string shows up. You can add a question mark at the end and it will erase the query string. Unfortunately the question mark stays.
If you happen to have multiple endpoints and not just play.php, you can do the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/play\.php* [NC, OR]
RewriteCond %{REQUEST_URI} ^/another_url\.php* [NC]
RewriteRule . /? [L,R=301]
</IfModule>
This RewriteRule will match play.php or another_url.php and do 301 redirect to your home page.
Alternatively:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(play|another_url)\.php* [NC]
RewriteRule . /? [L,R=301]
</IfModule>
Here is the alternative using the Redirect directive
Redirect 301 /play.php http://www.mysite.com/
Redirect 301 /another_url.php http://www.mysite.com/
Alternative you could also use the RedirectMatch directive to use regex expressions (Note I haven't actually tested this one)
RedirectMatch 301 /(play|another_url)\.php http://www.mysite.com/
EDIT
Ignore this answer. I misread your question. The following redirects all requests to index.php unless the file or directory exists, which is not what you want now that I read your question.
Here is how Wordpres does it. I believe Zend and Joomla also have variations of this.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Categories