url rewrite apache for wildcard domain - apache

I have a rewrite rule for wildcard domains all requests are send to index.php on main domain. This is content for my .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.us$
RewriteRule ^/?$ "http\:\/\/domain\.us\/" [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [NC,L]
And it works like it's supposed to work...perfect!
But recently I had to change the url structure of the whole website, that is fine old urls will simple show 404 though few old urls are important to me and I want to redirect them 301 to new url. For example:
somesubdomain.maindomain.com/oldcontent/
I want to reditect
somesubdomain.maindomain.com/importantdir/oldcontent/
Whatever I tried I ended up with a redirection loop (to many redirects at once).
I have like thousands of urls but only want to 301 redirect few of them.

Related

htaccess redirect cirtain requests to a different subdomain

I recently upgraded my website, changed the underlying system, and pushed the old content onto a subdomain.
I want the google indexed URLs to the old content to still work, so I added a rewrite rule to the main htaccess that I thought would redirect using looking for the old 2015 posts, but it doesn't appear to be working.
I have added the rule to the top of CraftCMS's default htaccess, which is at the root of my domain.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/2015(/.*)?$ http://old.keithcod.es/2015$1 [R=301,L]
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>
I clarify, I'm trying to redirect all traffic from /2015/* to old.keithcod.es/2015/*

htaccess - ReWrite one rule to another rule (virtual folder)

I have a file in root directory named propertyid.php
I have a RerwiteRule that works OK:
RewriteRule for-sale/(.*) propertyid.php?pid=$1
User enters this URL and same URL displays in address bar
domain.com/for-sale/(pid)
I want a second rule that will allow users to enter this url
domain.com/sell/(pid)
but I want the first URL to display in the address bar
domain.com/for-sale/(pid)
I have tried multiple variations of the following
RewriteRule ^sell$ /for-sale/propertyid.php?pid=$1
RewriteRule sell/(.*) for-sale?pid=$1
You need to redirect /sell/(.*) to /for-sale/(.*). Your redirect rule of /for-sale/(.*) to propertyid.php?pid=$1 will take care of the rest.
Just check few things:
/sell/(.*) to /for-sale/(.*) rule should come above
/for-sale/(.*) to propertyid.php?pid=$1 redirect rule and should
return 301 http code for moved permanently.
Check for multiple redirects.
Your .htaccess should look like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sell(.*)$ http://%{HTTP_HOST}/for-sale$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^for-sale/(.*)$ propertyid.php?pid=$1 [L]

htaccess rewrite after redirect

I have a site which uses htaccess to rewrite all pages to the index page with a hash which is then used to serve up content. The file looks like this....
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?urlpath=$1 [NC,L,QSA]
I am now moving some of the pages of the site, however if I add a redirect such as....
Redirect 301 /blog /new_location/blog/
I am running into problems with the resulting url looking like
https://mydomain/new_location/blog/urlpath=blog.php
Can anyone suggest a way that I get the page to redirect to mydomain/new_location/blog/ and then run the rewrite on the new url.
Many thanks
RewriteRule and Redirect are from different Apache modules, so run at different times in the processing, not in the order they appear in the configuration. You're best off sticking to one module or the other, by using the [R] flag to RewriteRule.
RewriteRule /blog(.*) /new_location/blog$1 [R=301]
OK, I managed to get this working using a combination of Redirect and Rewrite like so....
Redirect 301 /blog /new_location/blog
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/blog
RewriteRule ^([^?]*)$ /index.php?urlpath=$1 [NC,L,QSA]
Maybe not the neatest solution, but it work!

.htaccess redirect to WWW with current htaccess code?

I want to redirect my URLs to the www version. I can do this, but I already have working .htaccess code that is redirecting the browser to my index.php file where the URL is processed. I have this code (which I did not write), and I do not know enough about htaccess to figure the problem out:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [NC]
I have tried simply using the full URL path in the redirect, but this produces a 404 error when I attempt to access pages. I have also attempted to simply include more rules underneath this code, with no success.
Do you mean you want http://example.com/foo/bar to redirect to http://www.example.com/foo/bar?
If so, this should do the trick, while preserving your intent
RewriteEngine On
# First redirect non-www hosts (the "L" flag means we won't process
# any more rewrite conditions in this redirect; on the next request,
# the rewrite condition won't match and we'll fall through to your
# original rule)
RewriteCond %{HTTP_HOST} !^www\.(.*) [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,NC,L]
# Handle normal requests
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [NC]:

how can i rewrite all urls except for a few to https with mod rewrite

I have made a site with codeigniter below is the current htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I need to add some rules so that the site still works as normal but most URLs are redirected to https. The urls that need to be exempt from this rule are:
/about
/faqs
/terms-and-conditions
/contact-us
/privacy-policy
All of these urls that are exempt should be accessed over normal http
To redirect non-http request to https, use the %{HTTPS} in RewriteCond
RewriteCond %{HTTPS} off
If the request is https, this condition fails and the rule is skipped. Then to exempt paths, you can add them after
RewriteCond %{REQUEST_URI} !/about /faqs /terms-and-conditions /contact-us /privacy-policy
Then finally, the redirect
RewriteRule ^(.*)$ https://your-site.com/$1 [R,L]
You'll want to add these 3 lines above the rewrite conditions for the rule you have above that rewrites everything to index.php