.htaccess rewrite rules that include a sub-folder - .htaccess

Here is what I'm trying to do.
I have 2 apps running on my site one at root / and one at at /blog/.
I want to be able to access some pages that are served by the blog app but without the /blog/ part in the URL. This bit I have managed to do with the following rule.
RewriteRule my-page blog/index\.php?page_id=1 [L]
This allows me to view a page actually at /blog/my-page at /my-page.
Now where I'm struggling is with ensuring there are no duplicate URLs, so I'm trying to redirect to my shorter URL. Like so:
From:
/blog/my-page
To:
/my-page
From:
/blog/index.php?page_id=1
To:
/my-page
With the following rule, I can redirect from /index.php?page_id=1 to /my-page
RewriteCond %{QUERY_STRING} ^page_id=1$
RewriteRule index\.php$ my-page? [R=301,L]
After this rule happens the first rule I mentioned takes it to the right place.
My question is:
How can I get it to work with blog as part of the URL. I expected the below to work but it doesn't
#RewriteCond %{QUERY_STRING} ^page_id=1$
RewriteRule ^blog/index\.php$ my-page? [R=301,L]

The reason this was not working was because there was an .htaccess file within my /blog/ directory.
This included a rule that would catch anything and route to blog/index.php.
Moving my rules to /blog/.htaccess allowed them to function properly

Related

How to redirect my sudomains webspace to the actual subdomain?

I want to redirect https://example.com/sub/index.php to https://sub.example.com/index.php.
As you can tell, the subdomains webspace is in the actual domain webspace; this leads to duplicate content.
I already tried this:
Adding javascript to index.php's head:
<script type='text/javascript'>document.location.href='https://example.com/sub/index.php';</script>
But this creates an infinte loop.
Adding .htaccess to /sub/ directory:
RewriteEngine On
RewriteRule (.*) https://sub.example.com/index.php$1 [R=301,L]
But this makes it impossible to create other files in /sub/ because it always redirects you to index.php
What would be the best way to solve this if I also want to create other files in /sub/ which always redirect to the subdomain version?
The following placed at the root of your site should redirect anything that hits example.com/sub to sub.example.com.
RewriteEngine On
RewriteRule ^sub/(.*)$ http://sub.example.com/$1 [R=301,NC,L]
You can test it at https://htaccess.madewithlove.be/ if you need to tweak those rules.

.htaccess rewrite conflicting rules

I'm having an issue with some htaccess rules which I thought would be simple. I have some nice SEO friendly URL rewriting in place as below
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(index\.php|/images|/templates|/views|/ajax|/uploads|/robots\.txt|/sitemap\.xml|/favicon\.ico|/scripts|/cron|/combine.php|/js|/css)
RewriteRule ^(.*)$ index.php?ref=$1&%{QUERY_STRING} [L]
This all works well and I want to keep this. I also wish to rewrite some old pages which Google WMT is reporting as 404's to the new equivalent and for that I'd like to use:
Redirect 301 /about_us http://example.com/about-us
The problem I have is that the URL that the browser is directed to is:
http://example.com/about-us?ref=about_us
The about_us is the old link and about-us is the correct link. If the htaccess redirected to example.com/about-us then the other SEO friendly rewrite rule will pick it up and show the page but eh extra ?ref= parameter is confusing it. I am guessing the two rules are conflicting to a degree but is there a way to get the two rules to work together e.g. redirect without the extra ?ref= parameter? My knowledge of htaccess is basic to say the least so I am a little stuck on this one.
Thanks in advance
Redirect and RedirectMatch are part of mod_alias, while the rewrite rules are part of mod_rewrite. The problem you're running into is when you mix the two, both modules affect the same request, thus two things happen when you only want one. In this case, you need to stick with just mod_rewrite and use this instead:
RewriteEngine On
RewriteRule ^about_us /about-us [L,R=301]
RewriteCond %{REQUEST_URI} !^(index\.php|/images|/templates|/views|/ajax|/uploads|/robots\.txt|/sitemap\.xml|/favicon\.ico|/scripts|/cron|/combine.php|/js|/css)
RewriteRule ^(.*)$ index.php?ref=$1&%{QUERY_STRING} [L]
Note that the rule that redirects comes before the rule that routes to index.php.

Rewrite URL and remove part with .htaccess

I am really stuck with my .htaccess file, and need some help :). I have a WordPress installation that I am using for testing. It is in a folder and I use .htaccess to get there. This is the rules I use so far:
######### Custom #########
RewriteEngine On
# ignore folders
RewriteCond %{REQUEST_URI} "/af1wp/"
RewriteRule (.*) $1 [L]
###############
# only for me #
###############
# HOME (Senne Tijdeman)
RewriteCond %{REMOTE_ADDR} ^###\.###\.###\.###$
RewriteCond %{HTTP_HOST} ^((www.)?([a-z0-9_\-]+).)?alleenf1.nl$
RewriteCond %{REQUEST_URI} !^/af1wp/$
RewriteRule ^(.*)$ /af1wp/$1 [L]
This works (with my real IP address of course), so no problem there. But now I want to rewrite exisiting URL's to a new format. The old URL is this:
http://alleenf1.nl/nieuws/QOgbb/raikkonen-alles-is-mogelijk-in-australi
The new URL should be this:
http://alleenf1.nl/raikkonen-alles-is-mogelijk-in-australi
The part I want to remove "nieuws/QOgbb/" is not always the same, so I have to use regex for that. But everything I tried did not work at all.
I thought this would be simple, but apparently not for me unfortunately. Now I have 2 questions.
What is the right RewriteRule to do this?
Where should I put it. In the .htaccess of the root folder, or the af1wp folder where the WordPress install is?
Tnx in advanced
To awnser the questions from poncha below:
Yes, the URL's always start with to folders. Just to clarify (was not clear) the part "nieuws" is always the same, but not the second part (call it an ID).
I prefer a redirect.
The file /raikkonen-alles-is-mogelijk-in-australi is a post in WordPress. That WordPress installation currently resides in the folder af1wp, but will be moved to the root folder when going live.
Try this:
RewriteEngine On
RewriteRule ^nieuws/([^/]+)/(.*) /af1wp/$1 [R=301,L,QSA]
This will only match URLs starting with "nieuws"
For now, the rewrite target is /af1wp/, change it to / when moving the wordpress.
When you move wordpress, you'll need to mix in this rule inside the wordpress rules, as it already has rewrite rules of its own - place this rule above its rules.
The flags used here:
R=301 - redirect with HTTP status 301 (Moved Permanently).
L - last rule (stop rules parsing after successful match of this rule)
QSA - query-string-append (append original query string to the rewritten request).

How do I rewrite the url?

Could someone tell me how to rewrite this URL. I have looked at a lot of questions on stackoverflow but they seem to be missing my answer.
RewriteEngine On
That is what I have... its a bit poor.
I need to rewrite url's if they do not point to a directory.
I need to do this...
any.domain.com/pages/some-page-slug/login
To be rewritten to the correct url of...
any.domain.com/pages/login.php?page=32
Does anyone have any ideas on how this can be achieved?
1) Rewriting product.php?id=12 to product-12.html
It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
2) Rewriting product.php?id=12 to product/ipod-nano/12.html
SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.
RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
3) Redirecting non www URL to www URL
If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
5) Redirecting the domain to a new subfolder of inside public_html.
Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1
TO do this you need to write a front controller.
See here, here, here, and here.
Alternatively in Apache you can rewrite this
any.domain.com/pages/32/login
or this:
any.domain.com/32/login
or even this:
any.domain.com/some-slug/32/login
to this:
any.domain.com/pages/login.php?page=32
One way or another to do this with only apache you need to supply the page id in some fashion. Keep in mind even with format any.domain.com/some-slug/32/login the content of the slug is irrelevant and won't necessarily link to the correct page. Which I imagine is undesirable and bad for SEO.
Another alternative is using RewriteMap. But this will be tricky and require reloading apache configurations whenever a page/slug is created/edit.
I understand that pages and login are static in this case and some-page-slug is changing. And you always want to redirect to static page /pages/login.php?page=32
So this is how to do it:
1) Rewrite
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32
or 2) Redirect Pernament
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=301,L]
or 3) Redirect Temporary
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=302,L]
Here is great article about htaccess trics
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/

htaccess redirect for subdomains -> similarly-named subdirectories?

I'm restructuring a web site with a great deal of content currently parked at URLs that look like this.
http://string.domain.com/year/month/dd/string-pulled-from-title
For various reasons, I'd like to park all new content at URLs that looks like this
http://www.domain.com/blogs/string/year/month/dd/string-pulled-from-title
I'd like to make the change for future content, but don't want all the old stuff to go 404.
I believe a 301 redirect rule in my htaccess will do the trick, sending all referred traffic coming in through old links to the new formats.
But what should this rule look like? I've read a few tutorials but haven't found this exact case in any examples.
Note, I don't want to do this for all subdomains, only for about 10 specific ones. So if someone could help me figure out one of these, then I can copy paste it 10 times in my htaccess for each subdomain and be set.
Drop this into the .htaccess file of the old site (adjusting the domain to your actual one):
RewriteEngine On
RewriteRule ^(.*)$ http://example.com/blogs/string/$1 [R=301]
This will grab this part of the URL at the old site:
year/month/dd/string-pulled-from-title
and redirect it to the new site under the new location:
blogs/string/year/month/dd/string-pulled-from-title
Alternatively, if you want something a little more variable like, without having to custom fix each .htaccess, drop this in the file for each subdomain instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
If you're redirecting to the same domain, and it includes the www, adjust the rewrite rules to the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
Note the second RewriteCond which checks to make sure that the URL requested does not include the leading www, which may lead to an endless redirect if the destination URL itself includes www and would try and redirect that subdomain as well.
%1 grabs the first capture group from the line above.
$1 references the first capture group on the same line.

Resources