Mod-rewrite for domain except one directory - .htaccess

I am a novice at htaccess and can't figure out what's going on here. I am trying to get all redirects to point index.php with the PAGEID=pageName.
So, domain.com/manager would get pushed to index.php?PAGEID=manager.
Now, I have this working but I need it to just act normally when it hits the directory 'test', but whenever I goto domain.com/test I get pushed to index.php?PAGEID=test
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/test
RewriteRule ^([\w/]*)$ index.php?PAGEID=$1 [L]
Any ideas on how to get this working? What am I missing?

I think this logic is having some problems. Because in this example 'domain.com/test', we have write a condition as replace all strings(test) after domain.com to 'index.php?pageid=test'.
The problem is 'index.php' is also coming as a string after domain 'domain.com/index.php?pageid..' . Here also the rewrite condition will be applicable and it will rewrite it.
I think you should try something like this.
htaccess, redirect virtual subdomain to URL parameter
Or if 'test' is a static string, then in the rewrite condition we can specify as replace only string 'test' to 'index.php/pageid=test'

Related

htaccess rewrite url by matching only the first part of the url

I've been struggling to find a solution to this very simple problem. You would think that this is the classic "please help me I need to redirect this url to newurl" type of thing but, believe me, it isn't that easy.
I have a domain example.com, this example.com has some language variants:
example.com/mx
example.com/us
example.com/ca
example.com/cl
example.com/xx
and so on, example
.com is the index to choose between countries and it exists too but has no content.
We recently moved our example.com/mx to a whole new domain (with a different system) example.com.mx so we proceeded to permanently redirect every url there to the its new url in the example.com.mx
While we were at it, we discovered that our multilingual wordpress installation on example.com was duplicating all website's content in the root file, something which shouldn't have happened.
So, we have like
example.com/us/folder
example.com/cl/folder
example.com/ca/folder
etc
AND
example.com/folder which shouldn't exist
There are tons of directories that are duplicated there, we already know where to redirect each of these directories but we just can't find the right line of code to do it.
For example, we have set up this htaccess rule to deal with all the /mx/ folder, which works well:
RewriteRule ^mx/shop/ https://www.example.com.mx/? [L,R=301]
RewriteRule ^mx/home/ https://www.example.com.mx/? [L,R=301]
RewriteRule ^mx/page/ https://www.example.com.mx/? [L,R=301]
RewriteRule ^mx/tag/ https://www.example.com.mx/? [L,R=301]
But when trying to extend this same logic to the "/shop/" directory, it matches the same directory in EVERY country folder, something we obviously don't want.
This is what we tried, described previously:
RewriteRule ^/tag/ https://www.example.com.mx/newtagurl? [L,R=301]
This obviously also redirects example.com/ca/tag/, because the ^.
So my question is, how to properly redirect to these cases ONLY matching the beginning https://example.com/tag/ string instead of matching every /tag/ case in every country folder?
Thanks!

Using URL Rewrite and HTTP together

I'm trying to use URL rewriting, but only on a part of the address, though I'm new to this area of web development and I'm a bit confused.
The address is: domain.com/create.php?member=-1&hangout=1
But I'm trying to rewrite it to this: domain.com/create/?hangout=1
I'm getting the correct page, though I can't access "hangout" with $_GET['hangout'].
This is my rewriting code in my .htaccess document:
RewriteEngine On
RewriteRule ^create/ create.php?member=-1
Thank you.
If you add your own query string in a RewriteRule, you must use the QSA flag to append an existing query string
RewriteRule ^create/ create.php?member=-1 [QSA]
This will rewrite create/?hangout=7 to create.php and add both member=-1 and hangout=7 as the query string.

URL Rewriting subdomain

I'll be quick on what im trying to do,
basically I have a user profile page that will be my url, let's say,
profile.php?user=alex
So now what is working fine in my .htaccess file is changing that into
website.com/alex
for quicker access.
For other purpose, I would need that to be basically
alex.website.com
but I couldnt figure out a way to rewrite my URL to that, instead of having a subdomain for every user.
If you have any idea if it's possible & how I would go on doing this, I would appreciate it alot!
Thank you
Alex
To just rewrite the URL path, try this rule:
RewriteRule ^[a-z]+$ profile.php?user=$0
If your user names have a different syntax, replace [a-z] as you need.
For rewriting the host, try this rule:
RewriteCond %{HTTP_HOST} ^([a-z]+)\.example\.com$
RewriteRule ^$ profile.php?user=%1
Note that this will only rewrite //alex.example.com/ internally to //alex.example.com/profile.php?user=alex. Additionally, your server will already need to be configured that it accepts any subdomain of your domain (see ServerAlias and name-based virtual hosts).

.htaccess Rewrite Based on Existence of Path in URL

Here's the scenario, I have a website that used to be a static HTML site and WordPress blog using a subdomain (http://blog.domain.com).
I recently combined everything into a single WordPress installation. To maintain old links I had to rewrite requests like "http://blog.domain.com/index.php/2010/10/16/post-name" to "http://domain.com/index.php/2010/10/16/post-name". My problem is that when trying to visit just "http://blog.domain.com", I get redirected to "http://domain.com" when I want it to go to "http://domain.com/index.php/blog".
So, if a user requests "http://blog.domain.com" (by itself, with or without slash), I want it to go to "http://domain.com/index.php/blog". If they request an old URL of "http://blog.domain.com/some-link-to-a-post", I want it to redirect to "http://domain.com/some-link-to-a-post". In other words, if it's a URL to an actual post, I just want to strip the "blog" subdomain. If it's the old link to the main blog page, I want to remove the "blog" subdomain and append "/index.php/blog"
http://blog.domain.com/ -> http://domain.com/index.php/blog
http://blog.domain.com/index.php/2010/10/16/post-title -> http://domain.com/index.php/2010/10/16/post-title
Hopefully that's clear. I'm not an htaccess expert, so hopefully someone can help me out here. Thanks in advance!
Using the [L] command at the end of a rewrite will tell htaccess that this is the last rule it should match. If you put a rule to match your first condition at the top and the other rewrite rule you said you had already created after it, you should get your expected result.
Try this:
RewriteRule ^blog.domain.com(/?)$ domain.com/index.php/blog [L]
# Your other rewrite here #
I couldn't get that solution to work. However, I used the following:
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/index.php/blog/$1 [R=301,L]
That ends up in a URL like http://domain.com/index.php/blog/index.php/2010/06/04/post-title, but Wordpress is smart enough to fix it.

htaccess rewrite rule 'page numbers' problem

I've added a .htaccess file to my root folder for the purposes of rewriting dynamic URLs into static ones. This seems to have been done successfully but I am having problems with page numbers.
For example, if you were to visit a static page /widgets, the first page of the products is fine....but subsequent pages show up as /products.php?cat=27&pg=2 etc. What I want is for subsequent pages to be in the form of /widgets-pg2 or /widgets?pg=2.
Below is my rewrite rule that I used for the initial category page:-
RewriteRule ^widgets$ products.php?cat=27
If any of you experts can help with this, it would be much appreciated.
Are you expecting the cat to change as well? You'd need to account for that in your URL as well:
e.g. www.site.com/widgets/27/2 could be rewritten as:
RewriteRule ^widgets/([0-9]+)/([0-9]+)$ products.php?cat=$1&pg=$2
If widgets will always be cat 27 then you can change it to:
RewriteRule ^widgets$ products.php?cat=27 [QSA]
which is query string append
Try
RewriteRule ^widgets-pg(.+)$ products.php?cat=27&pg=$1
After that, go here :)
To allow a query string after your rewritten URL use the [QSA] flag:
RewriteRule ^widgets$ products.php?cat=27 [QSA]
A link would than be:
http://example.org/widgets?pg=167&perpage=100&tralala=Hi!
I tried the following but it resulted in a '404 Not Found' error when I go to /widgets:-
RewriteRule ^widgets-pg(.+)$ products.php?cat=27&pg=$1
And I tried the following:-
RewriteRule ^widgets$ products.php?cat=27 [QSA]
This worked correctly but to go to page two of the widgets page, I need to type in the browser:-
/widgets?pg=2
But the actual 'page 2' link still leads to:-
products.php?cat=27&pg=2
So apart from a rewrite rule....maybe I need a separate redirection rule? Or maybe need to change the page number links. But since these are dynamically generated, I'm not sure how to do this.
The following is the PHP code for the page:- http://freetexthost.com/3ubiydspzm

Resources